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