FFmpeg  4.4.8
ty.c
Go to the documentation of this file.
1 /*
2  * TiVo ty stream demuxer
3  * Copyright (c) 2005 VLC authors and VideoLAN
4  * Copyright (c) 2005 by Neal Symms (tivo@freakinzoo.com) - February 2005
5  * based on code by Christopher Wingert for tivo-mplayer
6  * tivo(at)wingert.org, February 2003
7  * Copyright (c) 2017 Paul B Mahol
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "libavutil/intreadwrite.h"
27 #include "avformat.h"
28 #include "internal.h"
29 #include "mpeg.h"
30 
31 #define SERIES1_PES_LENGTH 11 /* length of audio PES hdr on S1 */
32 #define SERIES2_PES_LENGTH 16 /* length of audio PES hdr on S2 */
33 #define AC3_PES_LENGTH 14 /* length of audio PES hdr for AC3 */
34 #define VIDEO_PES_LENGTH 16 /* length of video PES header */
35 #define DTIVO_PTS_OFFSET 6 /* offs into PES for MPEG PTS on DTivo */
36 #define SA_PTS_OFFSET 9 /* offset into PES for MPEG PTS on SA */
37 #define AC3_PTS_OFFSET 9 /* offset into PES for AC3 PTS on DTivo */
38 #define VIDEO_PTS_OFFSET 9 /* offset into PES for video PTS on all */
39 #define AC3_PKT_LENGTH 1536 /* size of TiVo AC3 pkts (w/o PES hdr) */
40 
41 static const uint8_t ty_VideoPacket[] = { 0x00, 0x00, 0x01, 0xe0 };
42 static const uint8_t ty_MPEGAudioPacket[] = { 0x00, 0x00, 0x01, 0xc0 };
43 static const uint8_t ty_AC3AudioPacket[] = { 0x00, 0x00, 0x01, 0xbd };
44 
45 #define TIVO_PES_FILEID 0xf5467abd
46 #define CHUNK_SIZE (128 * 1024)
47 #define CHUNK_PEEK_COUNT 3 /* number of chunks to probe */
48 
49 typedef struct TyRecHdr {
51  uint8_t ex[2];
54  uint64_t ty_pts; /* TY PTS in the record header */
55 } TyRecHdr;
56 
57 typedef enum {
62 
63 typedef enum {
68 
69 typedef enum {
74 
75 typedef struct TYDemuxContext {
76  unsigned cur_chunk;
77  unsigned cur_chunk_pos;
79  TiVo_type tivo_type; /* TiVo type (SA / DTiVo) */
80  TiVo_series tivo_series; /* Series1 or Series2 */
81  TiVo_audio audio_type; /* AC3 or MPEG */
82  int pes_length; /* Length of Audio PES header */
83  int pts_offset; /* offset into audio PES of PTS */
84  uint8_t pes_buffer[20]; /* holds incomplete pes headers */
85  int pes_buf_cnt; /* how many bytes in our buffer */
86  size_t ac3_pkt_size; /* length of ac3 pkt we've seen so far */
87  uint64_t last_ty_pts; /* last TY timestamp we've seen */
88 
92 
93  TyRecHdr *rec_hdrs; /* record headers array */
94  int cur_rec; /* current record in this chunk */
95  int num_recs; /* number of recs in this chunk */
97 
100 
101 static int ty_probe(const AVProbeData *p)
102 {
103  int i;
104 
105  for (i = 0; i + 12 < p->buf_size; i += CHUNK_SIZE) {
106  if (AV_RB32(p->buf + i) == TIVO_PES_FILEID &&
107  AV_RB32(p->buf + i + 4) == 0x02 &&
108  AV_RB32(p->buf + i + 8) == CHUNK_SIZE) {
109  return AVPROBE_SCORE_MAX;
110  }
111  }
112 
113  return 0;
114 }
115 
117  int num_recs)
118 {
119  TyRecHdr *hdrs, *rec_hdr;
120  int i;
121 
122  hdrs = av_calloc(num_recs, sizeof(TyRecHdr));
123  if (!hdrs)
124  return NULL;
125 
126  for (i = 0; i < num_recs; i++) {
127  const uint8_t *record_header = buf + (i * 16);
128 
129  rec_hdr = &hdrs[i]; /* for brevity */
130  rec_hdr->rec_type = record_header[3];
131  rec_hdr->subrec_type = record_header[2] & 0x0f;
132  if ((record_header[0] & 0x80) == 0x80) {
133  uint8_t b1, b2;
134 
135  /* marker bit 2 set, so read extended data */
136  b1 = (((record_header[0] & 0x0f) << 4) |
137  ((record_header[1] & 0xf0) >> 4));
138  b2 = (((record_header[1] & 0x0f) << 4) |
139  ((record_header[2] & 0xf0) >> 4));
140 
141  rec_hdr->ex[0] = b1;
142  rec_hdr->ex[1] = b2;
143  rec_hdr->rec_size = 0;
144  rec_hdr->ty_pts = 0;
145  } else {
146  rec_hdr->rec_size = (record_header[0] << 8 |
147  record_header[1]) << 4 |
148  (record_header[2] >> 4);
149  rec_hdr->ty_pts = AV_RB64(&record_header[8]);
150  }
151  }
152  return hdrs;
153 }
154 
155 static int find_es_header(const uint8_t *header,
156  const uint8_t *buffer, int search_len)
157 {
158  int count;
159 
160  for (count = 0; count < search_len; count++) {
161  if (!memcmp(&buffer[count], header, 4))
162  return count;
163  }
164  return -1;
165 }
166 
167 static int analyze_chunk(AVFormatContext *s, const uint8_t *chunk)
168 {
169  TYDemuxContext *ty = s->priv_data;
170  int num_recs, i;
171  TyRecHdr *hdrs;
172  int num_6e0, num_be0, num_9c0, num_3c0;
173 
174  /* skip if it's a Part header */
175  if (AV_RB32(&chunk[0]) == TIVO_PES_FILEID)
176  return 0;
177 
178  /* number of records in chunk (we ignore high order byte;
179  * rarely are there > 256 chunks & we don't need that many anyway) */
180  num_recs = chunk[0];
181  if (num_recs < 5) {
182  /* try again with the next chunk. Sometimes there are dead ones */
183  return 0;
184  }
185 
186  chunk += 4; /* skip past rec count & SEQ bytes */
187  ff_dlog(s, "probe: chunk has %d recs\n", num_recs);
188  hdrs = parse_chunk_headers(chunk, num_recs);
189  if (!hdrs)
190  return AVERROR(ENOMEM);
191 
192  /* scan headers.
193  * 1. check video packets. Presence of 0x6e0 means S1.
194  * No 6e0 but have be0 means S2.
195  * 2. probe for audio 0x9c0 vs 0x3c0 (AC3 vs Mpeg)
196  * If AC-3, then we have DTivo.
197  * If MPEG, search for PTS offset. This will determine SA vs. DTivo.
198  */
199  num_6e0 = num_be0 = num_9c0 = num_3c0 = 0;
200  for (i = 0; i < num_recs; i++) {
201  switch (hdrs[i].subrec_type << 8 | hdrs[i].rec_type) {
202  case 0x6e0:
203  num_6e0++;
204  break;
205  case 0xbe0:
206  num_be0++;
207  break;
208  case 0x3c0:
209  num_3c0++;
210  break;
211  case 0x9c0:
212  num_9c0++;
213  break;
214  }
215  }
216  ff_dlog(s, "probe: chunk has %d 0x6e0 recs, %d 0xbe0 recs.\n",
217  num_6e0, num_be0);
218 
219  /* set up our variables */
220  if (num_6e0 > 0) {
221  ff_dlog(s, "detected Series 1 Tivo\n");
224  } else if (num_be0 > 0) {
225  ff_dlog(s, "detected Series 2 Tivo\n");
228  }
229  if (num_9c0 > 0) {
230  ff_dlog(s, "detected AC-3 Audio (DTivo)\n");
235  } else if (num_3c0 > 0) {
237  ff_dlog(s, "detected MPEG Audio\n");
238  }
239 
240  /* if tivo_type still unknown, we can check PTS location
241  * in MPEG packets to determine tivo_type */
242  if (ty->tivo_type == TIVO_TYPE_UNKNOWN) {
243  uint32_t data_offset = 16 * num_recs;
244 
245  for (i = 0; i < num_recs; i++) {
246  if (data_offset + hdrs[i].rec_size > CHUNK_SIZE)
247  break;
248 
249  if ((hdrs[i].subrec_type << 8 | hdrs[i].rec_type) == 0x3c0 && hdrs[i].rec_size > 15) {
250  /* first make sure we're aligned */
251  int pes_offset = find_es_header(ty_MPEGAudioPacket,
252  &chunk[data_offset], 5);
253  if (pes_offset >= 0) {
254  /* pes found. on SA, PES has hdr data at offset 6, not PTS. */
255  if ((chunk[data_offset + 6 + pes_offset] & 0x80) == 0x80) {
256  /* S1SA or S2(any) Mpeg Audio (PES hdr, not a PTS start) */
257  if (ty->tivo_series == TIVO_SERIES1)
258  ff_dlog(s, "detected Stand-Alone Tivo\n");
259  ty->tivo_type = TIVO_TYPE_SA;
261  } else {
262  if (ty->tivo_series == TIVO_SERIES1)
263  ff_dlog(s, "detected DirecTV Tivo\n");
266  }
267  break;
268  }
269  }
270  data_offset += hdrs[i].rec_size;
271  }
272  }
273  av_free(hdrs);
274 
275  return 0;
276 }
277 
279 {
280  TYDemuxContext *ty = s->priv_data;
281  AVIOContext *pb = s->pb;
282  AVStream *st, *ast;
283  int i, ret = 0;
284 
288 
289  for (i = 0; i < CHUNK_PEEK_COUNT; i++) {
290  avio_read(pb, ty->chunk, CHUNK_SIZE);
291 
292  ret = analyze_chunk(s, ty->chunk);
293  if (ret < 0)
294  return ret;
295  if (ty->tivo_series != TIVO_SERIES_UNKNOWN &&
298  break;
299  }
300 
301  if (ty->tivo_series == TIVO_SERIES_UNKNOWN ||
304  return AVERROR(EIO);
305 
306  st = avformat_new_stream(s, NULL);
307  if (!st)
308  return AVERROR(ENOMEM);
312  avpriv_set_pts_info(st, 64, 1, 90000);
313 
314  ast = avformat_new_stream(s, NULL);
315  if (!ast)
316  return AVERROR(ENOMEM);
318 
319  if (ty->audio_type == TIVO_AUDIO_MPEG) {
322  } else {
324  }
325  avpriv_set_pts_info(ast, 64, 1, 90000);
326 
327  ty->first_chunk = 1;
328 
329  avio_seek(pb, 0, SEEK_SET);
330 
331  return 0;
332 }
333 
335 {
336  TYDemuxContext *ty = s->priv_data;
337  AVIOContext *pb = s->pb;
338  int read_size, num_recs;
339 
340  ff_dlog(s, "parsing ty chunk #%d\n", ty->cur_chunk);
341 
342  /* if we have left-over filler space from the last chunk, get that */
343  if (avio_feof(pb))
344  return AVERROR_EOF;
345 
346  /* read the TY packet header */
347  read_size = avio_read(pb, ty->chunk, CHUNK_SIZE);
348  ty->cur_chunk++;
349 
350  if ((read_size < 4) || (AV_RB32(ty->chunk) == 0)) {
351  return AVERROR_EOF;
352  }
353 
354  /* check if it's a PART Header */
355  if (AV_RB32(ty->chunk) == TIVO_PES_FILEID) {
356  /* skip master chunk and read new chunk */
357  return get_chunk(s);
358  }
359 
360  /* number of records in chunk (8- or 16-bit number) */
361  if (ty->chunk[3] & 0x80) {
362  /* 16 bit rec cnt */
363  ty->num_recs = num_recs = (ty->chunk[1] << 8) + ty->chunk[0];
364  } else {
365  /* 8 bit reclen - TiVo 1.3 format */
366  ty->num_recs = num_recs = ty->chunk[0];
367  }
368  ty->cur_rec = 0;
369  ty->first_chunk = 0;
370 
371  ff_dlog(s, "chunk has %d records\n", num_recs);
372  ty->cur_chunk_pos = 4;
373 
374  av_freep(&ty->rec_hdrs);
375 
376  if (num_recs * 16 >= CHUNK_SIZE - 4)
377  return AVERROR_INVALIDDATA;
378 
379  ty->rec_hdrs = parse_chunk_headers(ty->chunk + 4, num_recs);
380  if (!ty->rec_hdrs)
381  return AVERROR(ENOMEM);
382  ty->cur_chunk_pos += 16 * num_recs;
383 
384  return 0;
385 }
386 
388 {
389  TYDemuxContext *ty = s->priv_data;
390  const int subrec_type = rec_hdr->subrec_type;
391  const int64_t rec_size = rec_hdr->rec_size;
392  int es_offset1, ret;
393  int got_packet = 0;
394 
395  if (subrec_type != 0x02 && subrec_type != 0x0c &&
396  subrec_type != 0x08 && rec_size > 7) {
397 
398  /* get the PTS from this packet if it has one.
399  * on S1, only 0x06 has PES. On S2, however, most all do.
400  * Do NOT Pass the PES Header to the MPEG2 codec */
401  es_offset1 = find_es_header(ty_VideoPacket, ty->chunk + ty->cur_chunk_pos, 5);
402  if (es_offset1 != -1) {
403  if (rec_size < es_offset1 + VIDEO_PTS_OFFSET + 5)
404  return AVERROR_INVALIDDATA;
405 
407  ty->chunk + ty->cur_chunk_pos + es_offset1 + VIDEO_PTS_OFFSET);
408  if (subrec_type != 0x06) {
409  /* if we found a PES, and it's not type 6, then we're S2 */
410  /* The packet will have video data (& other headers) so we
411  * chop out the PES header and send the rest */
412  if (rec_size >= VIDEO_PES_LENGTH + es_offset1) {
413  int size = rec_hdr->rec_size - VIDEO_PES_LENGTH - es_offset1;
414 
415  ty->cur_chunk_pos += VIDEO_PES_LENGTH + es_offset1;
416  if ((ret = av_new_packet(pkt, size)) < 0)
417  return ret;
418  memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, size);
419  ty->cur_chunk_pos += size;
420  pkt->stream_index = 0;
421  got_packet = 1;
422  } else {
423  ff_dlog(s, "video rec type 0x%02x has short PES"
424  " (%"PRId64" bytes)\n", subrec_type, rec_size);
425  /* nuke this block; it's too short, but has PES marker */
426  ty->cur_chunk_pos += rec_size;
427  return 0;
428  }
429  }
430  }
431  }
432 
433  if (subrec_type == 0x06) {
434  /* type 6 (S1 DTivo) has no data, so we're done */
435  ty->cur_chunk_pos += rec_size;
436  return 0;
437  }
438 
439  if (!got_packet) {
440  if ((ret = av_new_packet(pkt, rec_size)) < 0)
441  return ret;
442  memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size);
443  ty->cur_chunk_pos += rec_size;
444  pkt->stream_index = 0;
445  got_packet = 1;
446  }
447 
448  /* if it's not a continue blk, then set PTS */
449  if (subrec_type != 0x02) {
450  if (subrec_type == 0x0c && pkt->size >= 6)
451  pkt->data[5] |= 0x08;
452  if (subrec_type == 0x07) {
453  ty->last_ty_pts = rec_hdr->ty_pts;
454  } else {
455  /* yes I know this is a cheap hack. It's the timestamp
456  used for display and skipping fwd/back, so it
457  doesn't have to be accurate to the millisecond.
458  I adjust it here by roughly one 1/30 sec. Yes it
459  will be slightly off for UK streams, but it's OK.
460  */
461  ty->last_ty_pts += 35000000;
462  //ty->last_ty_pts += 33366667;
463  }
464  /* set PTS for this block before we send */
465  if (ty->last_video_pts > AV_NOPTS_VALUE) {
466  pkt->pts = ty->last_video_pts;
467  /* PTS gets used ONCE.
468  * Any subsequent frames we get BEFORE next PES
469  * header will have their PTS computed in the codec */
471  }
472  }
473 
474  return got_packet;
475 }
476 
478  int32_t offset, int32_t rec_len)
479 {
480  TYDemuxContext *ty = s->priv_data;
481 
482  if (offset < 0 || offset + ty->pes_length > rec_len) {
483  /* entire PES header not present */
484  ff_dlog(s, "PES header at %"PRId32" not complete in record. storing.\n", offset);
485  /* save the partial pes header */
486  if (offset < 0) {
487  /* no header found, fake some 00's (this works, believe me) */
488  memset(ty->pes_buffer, 0, 4);
489  ty->pes_buf_cnt = 4;
490  if (rec_len > 4)
491  ff_dlog(s, "PES header not found in record of %"PRId32" bytes!\n", rec_len);
492  return -1;
493  }
494  /* copy the partial pes header we found */
495  memcpy(ty->pes_buffer, pkt->data + offset, rec_len - offset);
496  ty->pes_buf_cnt = rec_len - offset;
497 
498  if (offset > 0) {
499  /* PES Header was found, but not complete, so trim the end of this record */
500  pkt->size -= rec_len - offset;
501  return 1;
502  }
503  return -1; /* partial PES, no audio data */
504  }
505  /* full PES header present, extract PTS */
507  if (ty->first_audio_pts == AV_NOPTS_VALUE)
509  pkt->pts = ty->last_audio_pts;
510  memmove(pkt->data + offset, pkt->data + offset + ty->pes_length, rec_len - ty->pes_length);
511  pkt->size -= ty->pes_length;
512  return 0;
513 }
514 
516 {
517  TYDemuxContext *ty = s->priv_data;
518  const int subrec_type = rec_hdr->subrec_type;
519  const int64_t rec_size = rec_hdr->rec_size;
520  int es_offset1, ret;
521 
522  if (subrec_type == 2) {
523  int need = 0;
524  /* SA or DTiVo Audio Data, no PES (continued block)
525  * ================================================
526  */
527 
528  /* continue PES if previous was incomplete */
529  if (ty->pes_buf_cnt > 0) {
530  need = ty->pes_length - ty->pes_buf_cnt;
531 
532  ff_dlog(s, "continuing PES header\n");
533  /* do we have enough data to complete? */
534  if (need >= rec_size) {
535  /* don't have complete PES hdr; save what we have and return */
536  memcpy(ty->pes_buffer + ty->pes_buf_cnt, ty->chunk + ty->cur_chunk_pos, rec_size);
537  ty->cur_chunk_pos += rec_size;
538  ty->pes_buf_cnt += rec_size;
539  return 0;
540  }
541 
542  /* we have enough; reconstruct this frame with the new hdr */
543  memcpy(ty->pes_buffer + ty->pes_buf_cnt, ty->chunk + ty->cur_chunk_pos, need);
544  ty->cur_chunk_pos += need;
545  /* get the PTS out of this PES header (MPEG or AC3) */
546  if (ty->audio_type == TIVO_AUDIO_MPEG) {
547  es_offset1 = find_es_header(ty_MPEGAudioPacket,
548  ty->pes_buffer, 5);
549  } else {
550  es_offset1 = find_es_header(ty_AC3AudioPacket,
551  ty->pes_buffer, 5);
552  }
553  if (es_offset1 < 0) {
554  ff_dlog(s, "Can't find audio PES header in packet.\n");
555  } else {
557  &ty->pes_buffer[es_offset1 + ty->pts_offset]);
558  pkt->pts = ty->last_audio_pts;
559  }
560  ty->pes_buf_cnt = 0;
561 
562  }
563  if ((ret = av_new_packet(pkt, rec_size - need)) < 0)
564  return ret;
565  memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size - need);
566  ty->cur_chunk_pos += rec_size - need;
567  pkt->stream_index = 1;
568 
569  /* S2 DTivo has AC3 packets with 2 padding bytes at end. This is
570  * not allowed in the AC3 spec and will cause problems. So here
571  * we try to trim things. */
572  /* Also, S1 DTivo has alternating short / long AC3 packets. That
573  * is, one packet is short (incomplete) and the next packet has
574  * the first one's missing data, plus all of its own. Strange. */
575  if (ty->audio_type == TIVO_AUDIO_AC3 &&
576  ty->tivo_series == TIVO_SERIES2) {
577  if (ty->ac3_pkt_size + pkt->size > AC3_PKT_LENGTH) {
578  pkt->size -= 2;
579  ty->ac3_pkt_size = 0;
580  } else {
581  ty->ac3_pkt_size += pkt->size;
582  }
583  }
584  } else if (subrec_type == 0x03) {
585  if ((ret = av_new_packet(pkt, rec_size)) < 0)
586  return ret;
587  memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size);
588  ty->cur_chunk_pos += rec_size;
589  pkt->stream_index = 1;
590  /* MPEG Audio with PES Header, either SA or DTiVo */
591  /* ================================================ */
592  es_offset1 = find_es_header(ty_MPEGAudioPacket, pkt->data, 5);
593 
594  /* SA PES Header, No Audio Data */
595  /* ================================================ */
596  if ((es_offset1 == 0) && (rec_size == 16)) {
598  if (ty->first_audio_pts == AV_NOPTS_VALUE)
601  return 0;
602  }
603  /* DTiVo Audio with PES Header */
604  /* ================================================ */
605 
606  /* Check for complete PES */
607  if (check_sync_pes(s, pkt, es_offset1, rec_size) == -1) {
608  /* partial PES header found, nothing else.
609  * we're done. */
611  return 0;
612  }
613  } else if (subrec_type == 0x04) {
614  /* SA Audio with no PES Header */
615  /* ================================================ */
616  if ((ret = av_new_packet(pkt, rec_size)) < 0)
617  return ret;
618  memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size);
619  ty->cur_chunk_pos += rec_size;
620  pkt->stream_index = 1;
621  pkt->pts = ty->last_audio_pts;
622  } else if (subrec_type == 0x09) {
623  if ((ret = av_new_packet(pkt, rec_size)) < 0)
624  return ret;
625  memcpy(pkt->data, ty->chunk + ty->cur_chunk_pos, rec_size);
626  ty->cur_chunk_pos += rec_size ;
627  pkt->stream_index = 1;
628 
629  /* DTiVo AC3 Audio Data with PES Header */
630  /* ================================================ */
631  es_offset1 = find_es_header(ty_AC3AudioPacket, pkt->data, 5);
632 
633  /* Check for complete PES */
634  if (check_sync_pes(s, pkt, es_offset1, rec_size) == -1) {
635  /* partial PES header found, nothing else. we're done. */
637  return 0;
638  }
639  /* S2 DTivo has invalid long AC3 packets */
640  if (ty->tivo_series == TIVO_SERIES2) {
641  if (pkt->size > AC3_PKT_LENGTH) {
642  pkt->size -= 2;
643  ty->ac3_pkt_size = 0;
644  } else {
645  ty->ac3_pkt_size = pkt->size;
646  }
647  }
648  } else {
649  /* Unsupported/Unknown */
650  ty->cur_chunk_pos += rec_size;
651  return 0;
652  }
653 
654  return 1;
655 }
656 
658 {
659  TYDemuxContext *ty = s->priv_data;
660  AVIOContext *pb = s->pb;
661  TyRecHdr *rec;
662  int64_t rec_size = 0;
663  int ret = 0;
664 
665  if (avio_feof(pb))
666  return AVERROR_EOF;
667 
668  while (ret <= 0) {
669  if (!ty->rec_hdrs || ty->first_chunk || ty->cur_rec >= ty->num_recs) {
670  if (get_chunk(s) < 0 || ty->num_recs <= 0)
671  return AVERROR_EOF;
672  }
673 
674  rec = &ty->rec_hdrs[ty->cur_rec];
675  rec_size = rec->rec_size;
676  ty->cur_rec++;
677 
678  if (rec_size <= 0)
679  continue;
680 
681  if (ty->cur_chunk_pos + rec->rec_size > CHUNK_SIZE)
682  return AVERROR_INVALIDDATA;
683 
684  if (avio_feof(pb))
685  return AVERROR_EOF;
686 
687  switch (rec->rec_type) {
688  case VIDEO_ID:
689  ret = demux_video(s, rec, pkt);
690  break;
691  case AUDIO_ID:
692  ret = demux_audio(s, rec, pkt);
693  break;
694  default:
695  ff_dlog(s, "Invalid record type 0x%02x\n", rec->rec_type);
696  case 0x01:
697  case 0x02:
698  case 0x03: /* TiVo data services */
699  case 0x05: /* unknown, but seen regularly */
700  ty->cur_chunk_pos += rec->rec_size;
701  break;
702  }
703  }
704 
705  return 0;
706 }
707 
709 {
710  TYDemuxContext *ty = s->priv_data;
711 
712  av_freep(&ty->rec_hdrs);
713 
714  return 0;
715 }
716 
718  .name = "ty",
719  .long_name = NULL_IF_CONFIG_SMALL("TiVo TY Stream"),
720  .priv_data_size = sizeof(TYDemuxContext),
721  .read_probe = ty_probe,
725  .extensions = "ty,ty+",
727 };
uint8_t
int32_t
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:464
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:798
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:364
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RB32
Definition: intreadwrite.h:130
#define AV_RB64
Definition: intreadwrite.h:164
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
@ AV_CODEC_ID_MP2
Definition: codec_id.h:424
@ AV_CODEC_ID_AC3
Definition: codec_id.h:427
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
int i
Definition: input.c:407
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4945
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AUDIO_ID
Definition: mpeg.h:41
#define VIDEO_ID
Definition: mpeg.h:42
static int64_t ff_parse_pes_pts(const uint8_t *buf)
Parse MPEG-PES five-byte timestamp.
Definition: mpeg.h:68
static const uint8_t header[24]
Definition: sdr2.c:67
static char buffer[20]
Definition: seek.c:32
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int size
Definition: packet.h:370
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
enum AVStreamParseType need_parsing
Definition: avformat.h:1081
uint8_t chunk[CHUNK_SIZE]
Definition: ty.c:98
int64_t cur_pos
Definition: ty.c:78
TiVo_audio audio_type
Definition: ty.c:81
unsigned cur_chunk
Definition: ty.c:76
size_t ac3_pkt_size
Definition: ty.c:86
int cur_rec
Definition: ty.c:94
int64_t last_video_pts
Definition: ty.c:91
uint8_t pes_buffer[20]
Definition: ty.c:84
int pts_offset
Definition: ty.c:83
int pes_buf_cnt
Definition: ty.c:85
int64_t last_audio_pts
Definition: ty.c:90
int pes_length
Definition: ty.c:82
uint64_t last_ty_pts
Definition: ty.c:87
TiVo_series tivo_series
Definition: ty.c:80
TiVo_type tivo_type
Definition: ty.c:79
int num_recs
Definition: ty.c:95
TyRecHdr * rec_hdrs
Definition: ty.c:93
int64_t first_audio_pts
Definition: ty.c:89
unsigned cur_chunk_pos
Definition: ty.c:77
int first_chunk
Definition: ty.c:96
Definition: ty.c:49
int32_t rec_size
Definition: ty.c:50
uint64_t ty_pts
Definition: ty.c:54
uint8_t subrec_type
Definition: ty.c:53
uint8_t rec_type
Definition: ty.c:52
uint8_t ex[2]
Definition: ty.c:51
#define av_free(p)
#define ff_dlog(a,...)
#define av_freep(p)
AVPacket * pkt
Definition: movenc.c:59
int size
static int analyze_chunk(AVFormatContext *s, const uint8_t *chunk)
Definition: ty.c:167
static int check_sync_pes(AVFormatContext *s, AVPacket *pkt, int32_t offset, int32_t rec_len)
Definition: ty.c:477
static int ty_probe(const AVProbeData *p)
Definition: ty.c:101
#define DTIVO_PTS_OFFSET
Definition: ty.c:35
TiVo_series
Definition: ty.c:63
@ TIVO_SERIES_UNKNOWN
Definition: ty.c:64
@ TIVO_SERIES1
Definition: ty.c:65
@ TIVO_SERIES2
Definition: ty.c:66
static const uint8_t ty_MPEGAudioPacket[]
Definition: ty.c:42
static const uint8_t ty_AC3AudioPacket[]
Definition: ty.c:43
#define VIDEO_PTS_OFFSET
Definition: ty.c:38
#define TIVO_PES_FILEID
Definition: ty.c:45
static int ty_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ty.c:657
static int demux_audio(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
Definition: ty.c:515
#define VIDEO_PES_LENGTH
Definition: ty.c:34
AVInputFormat ff_ty_demuxer
Definition: ty.c:717
TiVo_audio
Definition: ty.c:69
@ TIVO_AUDIO_UNKNOWN
Definition: ty.c:70
@ TIVO_AUDIO_MPEG
Definition: ty.c:72
@ TIVO_AUDIO_AC3
Definition: ty.c:71
#define SA_PTS_OFFSET
Definition: ty.c:36
static int demux_video(AVFormatContext *s, TyRecHdr *rec_hdr, AVPacket *pkt)
Definition: ty.c:387
#define AC3_PTS_OFFSET
Definition: ty.c:37
#define SERIES1_PES_LENGTH
Definition: ty.c:31
static int get_chunk(AVFormatContext *s)
Definition: ty.c:334
static const uint8_t ty_VideoPacket[]
Definition: ty.c:41
TiVo_type
Definition: ty.c:57
@ TIVO_TYPE_DTIVO
Definition: ty.c:60
@ TIVO_TYPE_SA
Definition: ty.c:59
@ TIVO_TYPE_UNKNOWN
Definition: ty.c:58
static int ty_read_header(AVFormatContext *s)
Definition: ty.c:278
#define AC3_PES_LENGTH
Definition: ty.c:33
#define AC3_PKT_LENGTH
Definition: ty.c:39
#define SERIES2_PES_LENGTH
Definition: ty.c:32
static TyRecHdr * parse_chunk_headers(const uint8_t *buf, int num_recs)
Definition: ty.c:116
static int find_es_header(const uint8_t *header, const uint8_t *buffer, int search_len)
Definition: ty.c:155
#define CHUNK_SIZE
Definition: ty.c:46
static int ty_read_close(AVFormatContext *s)
Definition: ty.c:708
#define CHUNK_PEEK_COUNT
Definition: ty.c:47
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:1665
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:1666