FFmpeg
dv.c
Go to the documentation of this file.
1 /*
2  * General DV demuxer
3  * Copyright (c) 2003 Roman Shaposhnik
4  *
5  * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
6  * of DV technical info.
7  *
8  * Raw DV format
9  * Copyright (c) 2002 Fabrice Bellard
10  *
11  * 50 Mbps (DVCPRO50) and 100 Mbps (DVCPRO HD) support
12  * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
13  * Funded by BBC Research & Development
14  *
15  * This file is part of FFmpeg.
16  *
17  * FFmpeg is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  *
22  * FFmpeg is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  * Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with FFmpeg; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31 
32 #include "config_components.h"
33 
34 #include <time.h>
35 #include "avformat.h"
36 #include "demux.h"
37 #include "internal.h"
38 #include "libavcodec/dv_profile.h"
39 #include "libavcodec/dv.h"
41 #include "libavutil/intreadwrite.h"
42 #include "libavutil/mathematics.h"
43 #include "libavutil/timecode.h"
44 #include "dv.h"
45 #include "libavutil/avassert.h"
46 
47 #if CONFIG_DV_DEMUXER
48 
49 // Must be kept in sync with AVPacket
50 typedef struct DVPacket {
51  int64_t pts;
52  uint8_t *data;
53  int size;
54  int stream_index;
55  int flags;
56  int64_t pos;
58 
59  int sample_rate;
60  int last_sample_rate;
61 } DVPacket;
62 
63 struct DVDemuxContext {
64  const AVDVProfile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */
65  AVFormatContext* fctx;
66  AVStream* vst;
67  AVStream* ast[4];
68  struct DVPacket audio_pkt[4];
69  uint8_t audio_buf[4][8192];
70  int ach;
71  int frames;
72 
73  int64_t next_pts_video;
74  int64_t next_pts_audio;
75 };
76 
77 static inline uint16_t dv_audio_12to16(uint16_t sample)
78 {
79  uint16_t shift, result;
80 
81  sample = (sample < 0x800) ? sample : sample | 0xf000;
82  shift = (sample & 0xf00) >> 8;
83 
84  if (shift < 0x2 || shift > 0xd) {
85  result = sample;
86  } else if (shift < 0x8) {
87  shift--;
88  result = (sample - (256 * shift)) << shift;
89  } else {
90  shift = 0xe - shift;
91  result = ((sample + ((256 * shift) + 1)) << shift) - 1;
92  }
93 
94  return result;
95 }
96 
97 static const uint8_t *dv_extract_pack(const uint8_t *frame, enum DVPackType t)
98 {
99  int offs;
100  int c;
101 
102  for (c = 0; c < 10; c++) {
103  switch (t) {
104  case DV_AUDIO_SOURCE:
105  if (c&1) offs = (80 * 6 + 80 * 16 * 0 + 3 + c*12000);
106  else offs = (80 * 6 + 80 * 16 * 3 + 3 + c*12000);
107  break;
108  case DV_AUDIO_CONTROL:
109  if (c&1) offs = (80 * 6 + 80 * 16 * 1 + 3 + c*12000);
110  else offs = (80 * 6 + 80 * 16 * 4 + 3 + c*12000);
111  break;
112  case DV_VIDEO_CONTROL:
113  if (c&1) offs = (80 * 3 + 8 + c*12000);
114  else offs = (80 * 5 + 48 + 5 + c*12000);
115  break;
116  case DV_TIMECODE:
117  offs = (80*1 + 3 + 3);
118  break;
119  default:
120  return NULL;
121  }
122  if (frame[offs] == t)
123  break;
124  }
125 
126  return frame[offs] == t ? &frame[offs] : NULL;
127 }
128 
129 static const int dv_audio_frequency[3] = {
130  48000, 44100, 32000,
131 };
132 
133 /*
134  * There's a couple of assumptions being made here:
135  * 1. By default we silence erroneous (0x8000/16-bit 0x800/12-bit) audio samples.
136  * We can pass them upwards when libavcodec will be ready to deal with them.
137  * 2. We don't do software emphasis.
138  * 3. Audio is always returned as 16-bit linear samples: 12-bit nonlinear samples
139  * are converted into 16-bit linear ones.
140  */
141 static int dv_extract_audio(const uint8_t *frame, uint8_t **ppcm,
142  const AVDVProfile *sys)
143 {
144  int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
145  uint16_t lc, rc;
146  const uint8_t *as_pack;
147  uint8_t *pcm, ipcm;
148 
149  as_pack = dv_extract_pack(frame, DV_AUDIO_SOURCE);
150  if (!as_pack) /* No audio ? */
151  return 0;
152 
153  smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
154  freq = as_pack[4] >> 3 & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
155  quant = as_pack[4] & 0x07; /* 0 - 16-bit linear, 1 - 12-bit nonlinear */
156 
157  if (quant > 1)
158  return -1; /* unsupported quantization */
159 
160  if (freq >= FF_ARRAY_ELEMS(dv_audio_frequency))
161  return AVERROR_INVALIDDATA;
162 
163  size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
164  half_ch = sys->difseg_size / 2;
165 
166  /* We work with 720p frames split in half, thus even frames have
167  * channels 0,1 and odd 2,3. */
168  ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
169 
170  if (ipcm + sys->n_difchan > (quant == 1 ? 2 : 4)) {
171  av_log(NULL, AV_LOG_ERROR, "too many dv pcm frames\n");
172  return AVERROR_INVALIDDATA;
173  }
174 
175  /* for each DIF channel */
176  for (chan = 0; chan < sys->n_difchan; chan++) {
177  av_assert0(ipcm<4);
178  pcm = ppcm[ipcm++];
179  if (!pcm)
180  break;
181 
182  /* for each DIF segment */
183  for (i = 0; i < sys->difseg_size; i++) {
184  frame += 6 * 80; /* skip DIF segment header */
185  if (quant == 1 && i == half_ch) {
186  /* next stereo channel (12-bit mode only) */
187  av_assert0(ipcm<4);
188  pcm = ppcm[ipcm++];
189  if (!pcm)
190  break;
191  }
192 
193  /* for each AV sequence */
194  for (j = 0; j < 9; j++) {
195  for (d = 8; d < 80; d += 2) {
196  if (quant == 0) { /* 16-bit quantization */
197  of = sys->audio_shuffle[i][j] +
198  (d - 8) / 2 * sys->audio_stride;
199  if (of * 2 >= size)
200  continue;
201 
202  /* FIXME: maybe we have to admit that DV is a
203  * big-endian PCM */
204  pcm[of * 2] = frame[d + 1];
205  pcm[of * 2 + 1] = frame[d];
206 
207  if (pcm[of * 2 + 1] == 0x80 && pcm[of * 2] == 0x00)
208  pcm[of * 2 + 1] = 0;
209  } else { /* 12-bit quantization */
210  lc = ((uint16_t)frame[d] << 4) |
211  ((uint16_t)frame[d + 2] >> 4);
212  rc = ((uint16_t)frame[d + 1] << 4) |
213  ((uint16_t)frame[d + 2] & 0x0f);
214  lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
215  rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
216 
217  of = sys->audio_shuffle[i % half_ch][j] +
218  (d - 8) / 3 * sys->audio_stride;
219  if (of * 2 >= size)
220  continue;
221 
222  /* FIXME: maybe we have to admit that DV is a
223  * big-endian PCM */
224  pcm[of * 2] = lc & 0xff;
225  pcm[of * 2 + 1] = lc >> 8;
226  of = sys->audio_shuffle[i % half_ch + half_ch][j] +
227  (d - 8) / 3 * sys->audio_stride;
228  /* FIXME: maybe we have to admit that DV is a
229  * big-endian PCM */
230  pcm[of * 2] = rc & 0xff;
231  pcm[of * 2 + 1] = rc >> 8;
232  ++d;
233  }
234  }
235 
236  frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
237  }
238  }
239  }
240 
241  return size;
242 }
243 
244 static int dv_extract_audio_info(DVDemuxContext *c, const uint8_t *frame)
245 {
246  const uint8_t *as_pack;
247  int freq, stype, smpls, quant, i, ach, sr;
248 
249  as_pack = dv_extract_pack(frame, DV_AUDIO_SOURCE);
250  if (!as_pack || !c->sys) { /* No audio ? */
251  c->ach = 0;
252  return 0;
253  }
254 
255  smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
256  freq = as_pack[4] >> 3 & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
257  stype = as_pack[3] & 0x1f; /* 0 - 2CH, 2 - 4CH, 3 - 8CH */
258  quant = as_pack[4] & 0x07; /* 0 - 16-bit linear, 1 - 12-bit nonlinear */
259 
260  if (freq >= FF_ARRAY_ELEMS(dv_audio_frequency)) {
261  av_log(c->fctx, AV_LOG_ERROR,
262  "Unrecognized audio sample rate index (%d)\n", freq);
263  return 0;
264  }
265  sr = dv_audio_frequency[freq];
266 
267  if (stype > 3) {
268  av_log(c->fctx, AV_LOG_ERROR, "stype %d is invalid\n", stype);
269  c->ach = 0;
270  return 0;
271  }
272 
273  /* note: ach counts PAIRS of channels (i.e. stereo channels) */
274  ach = ((int[4]) { 1, 0, 2, 4 })[stype];
275  if (ach == 1 && quant && freq == 2)
276  ach = 2;
277 
278  /* Dynamic handling of the audio streams in DV */
279  c->ach = 0;
280  for (i = 0; i < ach; i++) {
281  if (!c->ast[i]) {
282  c->ast[i] = avformat_new_stream(c->fctx, NULL);
283  if (!c->ast[i])
284  return AVERROR(ENOMEM);
285 
286  avpriv_set_pts_info(c->ast[i], 64, 1, DV_TIMESCALE_AUDIO);
287  c->ast[i]->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
288  c->ast[i]->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
289  c->ast[i]->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
290  c->ast[i]->start_time = 0;
291  c->ast[i]->codecpar->bit_rate = 2 * sr * 16;
292 
293  c->ast[i]->codecpar->sample_rate = sr;
294  c->audio_pkt[i].last_sample_rate = sr;
295 
296  c->audio_pkt[i].size = 0;
297  c->audio_pkt[i].data = c->audio_buf[i];
298  c->audio_pkt[i].stream_index = c->ast[i]->index;
299  c->audio_pkt[i].flags |= AV_PKT_FLAG_KEY;
300  c->audio_pkt[i].pts = AV_NOPTS_VALUE;
301  c->audio_pkt[i].duration = 0;
302  c->audio_pkt[i].pos = -1;
303  }
304 
305  c->audio_pkt[i].sample_rate = sr;
306  }
307  c->ach = ach;
308 
309  return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
310 }
311 
312 static int dv_extract_video_info(DVDemuxContext *c, const uint8_t *frame)
313 {
314  const uint8_t *vsc_pack;
315  AVCodecParameters *par;
316  int apt, is16_9;
317 
318  par = c->vst->codecpar;
319 
320  c->vst->avg_frame_rate = av_inv_q(c->vst->time_base);
321 
322  /* finding out SAR is a little bit messy */
323  vsc_pack = dv_extract_pack(frame, DV_VIDEO_CONTROL);
324  apt = frame[4] & 0x07;
325  is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
326  (!apt && (vsc_pack[2] & 0x07) == 0x07)));
327  c->vst->sample_aspect_ratio = c->sys->sar[is16_9];
328  par->bit_rate = av_rescale_q(c->sys->frame_size,
329  (AVRational) { 8, 1 },
330  c->sys->time_base);
331  return c->sys->frame_size;
332 }
333 
334 static int dv_extract_timecode(DVDemuxContext* c, const uint8_t* frame, char *tc)
335 {
336  const uint8_t *tc_pack;
337 
338  // For PAL systems, drop frame bit is replaced by an arbitrary
339  // bit so its value should not be considered. Drop frame timecode
340  // is only relevant for NTSC systems.
341  int prevent_df = c->sys->ltc_divisor == 25 || c->sys->ltc_divisor == 50;
342 
343  tc_pack = dv_extract_pack(frame, DV_TIMECODE);
344  if (!tc_pack)
345  return 0;
346  av_timecode_make_smpte_tc_string2(tc, av_inv_q(c->sys->time_base), AV_RB32(tc_pack + 1), prevent_df, 1);
347  return 1;
348 }
349 
350 /* The following 3 functions constitute our interface to the world */
351 
352 static int dv_init_demux(AVFormatContext *s, DVDemuxContext *c)
353 {
354  c->vst = avformat_new_stream(s, NULL);
355  if (!c->vst)
356  return AVERROR(ENOMEM);
357 
358  c->fctx = s;
359  c->vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
360  c->vst->codecpar->codec_id = AV_CODEC_ID_DVVIDEO;
361  c->vst->start_time = 0;
362 
364 
365  /* Audio streams are added later as they are encountered. */
366  s->ctx_flags |= AVFMTCTX_NOHEADER;
367 
368  return 0;
369 }
370 
372 {
373  DVDemuxContext *c;
374 
375  c = av_mallocz(sizeof(DVDemuxContext));
376  if (!c)
377  return NULL;
378 
379  if (dv_init_demux(s, c)) {
380  av_free(c);
381  return NULL;
382  }
383 
384  return c;
385 }
386 
388 {
389  int size = -1;
390  int i;
391 
392  for (i = 0; i < c->ach; i++) {
393  if (c->ast[i] && c->audio_pkt[i].size) {
394  DVPacket *dpkt = &c->audio_pkt[i];
395 
396  pkt->size = dpkt->size;
397  pkt->data = dpkt->data;
398  pkt->stream_index = dpkt->stream_index;
399  pkt->flags = dpkt->flags;
400  pkt->pts = dpkt->pts;
401  pkt->duration = dpkt->duration;
402  pkt->pos = dpkt->pos;
403 
404  dpkt->size = 0;
405  size = pkt->size;
406 
407  if (dpkt->last_sample_rate != dpkt->sample_rate) {
408  int ret = ff_add_param_change(pkt, 0, 0, dpkt->sample_rate, 0, 0);
409  if (ret < 0)
410  return ret;
411  dpkt->last_sample_rate = dpkt->sample_rate;
412  }
413 
414  break;
415  }
416  }
417 
418  return size;
419 }
420 
422  uint8_t *buf, int buf_size, int64_t pos)
423 {
425  int size, i;
426  uint8_t *ppcm[5] = { 0 };
427 
428  if (buf_size < DV_PROFILE_BYTES ||
429  !(c->sys = av_dv_frame_profile(c->sys, buf, buf_size)) ||
430  buf_size < c->sys->frame_size) {
431  return AVERROR_INVALIDDATA;
432  }
433 
434  /* Queueing audio packet */
435  /* FIXME: in case of no audio/bad audio we have to do something */
436  size = dv_extract_audio_info(c, buf);
437  if (size < 0)
438  return size;
439 
440  if (c->ach) {
441  int64_t next_pts_video = av_rescale_q(c->next_pts_video, c->vst->time_base,
442  c->ast[0]->time_base);
443 
444  duration = av_rescale_q(size / 4,
445  (AVRational){ 1, c->audio_pkt[0].sample_rate },
446  c->ast[0]->time_base);
447 
448  // if audio timestamps are more than one frame away from video,
449  // assume desync happened (e.g. due to dropped audio frames) and
450  // resynchronize
451  pts = (FFABS(next_pts_video - c->next_pts_audio) >= duration) ?
452  next_pts_video : c->next_pts_audio;
453 
454  c->next_pts_audio = pts + duration;
455  }
456 
457  for (i = 0; i < c->ach; i++) {
458  DVPacket *dpkt = &c->audio_pkt[i];
459 
460  dpkt->pos = pos;
461  dpkt->size = size;
462  dpkt->pts = pts;
463  dpkt->duration = duration;
464 
465  ppcm[i] = c->audio_buf[i];
466  }
467  if (c->ach)
468  dv_extract_audio(buf, ppcm, c->sys);
469 
470  /* We work with 720p frames split in half, thus even frames have
471  * channels 0,1 and odd 2,3. */
472  if (c->sys->height == 720) {
473  if (buf[1] & 0x0C) {
474  c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
475  } else {
476  c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
477  }
478  }
479 
480  /* return the video packet, if the caller wants it */
481  if (pkt) {
482  size = dv_extract_video_info(c, buf);
483 
484  pkt->data = buf;
485  pkt->pos = pos;
486  pkt->size = size;
488  pkt->stream_index = c->vst->index;
489  pkt->pts = c->next_pts_video;
490  pkt->duration = av_rescale_q(1, c->sys->time_base, c->vst->time_base);
491 
492  c->next_pts_video += pkt->duration;
493  }
494 
495  c->frames++;
496 
497  return size;
498 }
499 
500 static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
501  int64_t *timestamp)
502 {
503  // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
504  FFFormatContext *const si = ffformatcontext(s);
505  const int frame_size = c->sys->frame_size;
506  int64_t frame_count = av_rescale_q(*timestamp, c->vst->time_base, c->sys->time_base);
507  int64_t offset;
508  int64_t size = avio_size(s->pb) - si->data_offset;
509  int64_t max_offset = ((size - 1) / frame_size) * frame_size;
510 
511  offset = frame_size * frame_count;
512 
513  if (size >= 0 && offset > max_offset)
514  offset = max_offset;
515  else if (offset < 0)
516  offset = 0;
517 
518  *timestamp = av_rescale_q(offset / frame_size, c->sys->time_base, c->vst->time_base);
519 
520  return offset + si->data_offset;
521 }
522 
524 {
525  c->frames = !c->sys ? 0 :
526  av_rescale_q(ts, c->vst->time_base, c->sys->time_base);
527  c->next_pts_video = ts;
528  c->next_pts_audio = (!c->sys || !c->ast[0]) ? AV_NOPTS_VALUE :
529  av_rescale_q(ts, c->vst->time_base, c->ast[0]->time_base);
530 
531  c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
532  c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
533 }
534 
535 /************************************************************
536  * Implementation of the easiest DV storage of all -- raw DV.
537  ************************************************************/
538 
539 typedef struct RawDVContext {
540  DVDemuxContext dv_demux;
541  uint8_t buf[DV_MAX_FRAME_SIZE];
542 } RawDVContext;
543 
544 static int dv_read_timecode(AVFormatContext *s) {
545  int ret;
546  char timecode[AV_TIMECODE_STR_SIZE];
547  int64_t pos = avio_tell(s->pb);
548 
549  // Read 3 DIF blocks: Header block and 2 Subcode blocks.
550 #define PARTIAL_FRAME_SIZE (3 * 80)
551  uint8_t partial_frame[PARTIAL_FRAME_SIZE];
552  RawDVContext *c = s->priv_data;
553 
554  ret = avio_read(s->pb, partial_frame, PARTIAL_FRAME_SIZE);
555  if (ret < 0)
556  goto finish;
557 
558  if (ret < PARTIAL_FRAME_SIZE) {
559  ret = -1;
560  goto finish;
561  }
562 
563  ret = dv_extract_timecode(&c->dv_demux, partial_frame, timecode);
564  if (ret)
565  av_dict_set(&s->metadata, "timecode", timecode, 0);
566  else
567  av_log(s, AV_LOG_ERROR, "Detected timecode is invalid\n");
568 
569 finish:
570  avio_seek(s->pb, pos, SEEK_SET);
571  return ret;
572 }
573 
574 static int dv_read_header(AVFormatContext *s)
575 {
576  unsigned state, marker_pos = 0;
577  RawDVContext *c = s->priv_data;
578  int ret;
579 
580  if ((ret = dv_init_demux(s, &c->dv_demux)) < 0)
581  return ret;
582 
583  state = avio_rb32(s->pb);
584  while ((state & 0xffffff7f) != 0x1f07003f) {
585  if (avio_feof(s->pb)) {
586  av_log(s, AV_LOG_ERROR, "Cannot find DV header.\n");
587  return AVERROR_INVALIDDATA;
588  }
589  if (state == 0x003f0700 || state == 0xff3f0700)
590  marker_pos = avio_tell(s->pb);
591  if (state == 0xff3f0701 && avio_tell(s->pb) - marker_pos == 80) {
592  avio_seek(s->pb, -163, SEEK_CUR);
593  state = avio_rb32(s->pb);
594  break;
595  }
596  state = (state << 8) | avio_r8(s->pb);
597  }
598  AV_WB32(c->buf, state);
599 
600  if (avio_read(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) != DV_PROFILE_BYTES - 4 ||
601  avio_seek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0) {
602  return AVERROR(EIO);
603  }
604 
605  c->dv_demux.sys = av_dv_frame_profile(c->dv_demux.sys,
606  c->buf,
608  if (!c->dv_demux.sys) {
610  "Can't determine profile of DV input stream.\n");
611  return AVERROR_INVALIDDATA;
612  }
613 
614  s->bit_rate = av_rescale_q(c->dv_demux.sys->frame_size,
615  (AVRational) { 8, 1 },
616  c->dv_demux.sys->time_base);
617 
618  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
619  dv_read_timecode(s);
620 
621  return 0;
622 }
623 
624 static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
625 {
626  int size;
627  RawDVContext *c = s->priv_data;
628 
629  size = avpriv_dv_get_packet(&c->dv_demux, pkt);
630 
631  if (size < 0) {
632  int ret;
633  int64_t pos = avio_tell(s->pb);
634  if (!c->dv_demux.sys)
635  return AVERROR(EIO);
636  size = c->dv_demux.sys->frame_size;
637  ret = avio_read(s->pb, c->buf, size);
638  if (ret < 0) {
639  return ret;
640  } else if (ret == 0) {
641  return AVERROR(EIO);
642  }
643 
644  size = avpriv_dv_produce_packet(&c->dv_demux, pkt, c->buf, size, pos);
645  }
646 
647  return size;
648 }
649 
650 static int dv_read_seek(AVFormatContext *s, int stream_index,
651  int64_t timestamp, int flags)
652 {
653  RawDVContext *r = s->priv_data;
654  DVDemuxContext *c = &r->dv_demux;
655  int64_t offset;
656 
657  // seek using the video stream
658  if (stream_index != c->vst->index)
659  timestamp = av_rescale_q(timestamp, s->streams[stream_index]->time_base,
660  c->vst->time_base);
661 
662  offset = dv_frame_offset(s, c, &timestamp);
663 
664  if (avio_seek(s->pb, offset, SEEK_SET) < 0)
665  return -1;
666 
667  ff_dv_ts_reset(c, timestamp);
668  return 0;
669 }
670 
671 static int dv_probe(const AVProbeData *p)
672 {
673  unsigned marker_pos = 0;
674  int i;
675  int matches = 0;
676  int firstmatch = 0;
677  int secondary_matches = 0;
678 
679  if (p->buf_size < 5)
680  return 0;
681 
682  for (i = 0; i < p->buf_size-4; i++) {
683  unsigned state = AV_RB32(p->buf+i);
684  if ((state & 0x0007f840) == 0x00070000) {
685  // any section header, also with seq/chan num != 0,
686  // should appear around every 12000 bytes, at least 10 per frame
687  if ((state & 0xff07ff7f) == 0x1f07003f) {
688  secondary_matches++;
689  if ((state & 0xffffff7f) == 0x1f07003f) {
690  matches++;
691  if (!i)
692  firstmatch = 1;
693  }
694  }
695  if (state == 0x003f0700 || state == 0xff3f0700)
696  marker_pos = i;
697  if (state == 0xff3f0701 && i - marker_pos == 80)
698  matches++;
699  }
700  }
701 
702  if (matches && p->buf_size / matches < 1024 * 1024) {
703  if (matches > 4 || firstmatch ||
704  (secondary_matches >= 10 &&
705  p->buf_size / secondary_matches < 24000))
706  // not max to avoid dv in mov to match
707  return AVPROBE_SCORE_MAX * 3 / 4;
708  return AVPROBE_SCORE_MAX / 4;
709  }
710  return 0;
711 }
712 
714  .p.name = "dv",
715  .p.long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
716  .p.extensions = "dv,dif",
717  .priv_data_size = sizeof(RawDVContext),
718  .read_probe = dv_probe,
719  .read_header = dv_read_header,
720  .read_packet = dv_read_packet,
721  .read_seek = dv_read_seek,
722 };
723 
724 #else // CONFIG_DV_DEMUXER
726 {
727  return NULL;
728 }
729 
731 {
732  return AVERROR(ENOSYS);
733 }
734 
736  uint8_t *buf, int buf_size, int64_t pos)
737 {
738  return AVERROR(ENOSYS);
739 }
740 #endif // CONFIG_DV_DEMUXER
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:328
AV_TIMECODE_STR_SIZE
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
DV_AUDIO_CONTROL
@ DV_AUDIO_CONTROL
Definition: dv.h:43
r
const char * r
Definition: vf_curves.c:126
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
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:379
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:188
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int64_t
long long int64_t
Definition: coverity.c:34
DVPacket
For DV, one packet corresponds exactly to one frame.
Definition: iec61883.c:55
AVPacket::data
uint8_t * data
Definition: packet.h:522
data
const char data[16]
Definition: mxf.c:148
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
mathematics.h
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
DV_PROFILE_BYTES
#define DV_PROFILE_BYTES
Definition: dv_profile.h:29
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:322
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:577
sample_rate
sample_rate
Definition: ffmpeg_filter.c:409
DVDemuxContext
struct DVDemuxContext DVDemuxContext
Definition: dv.h:33
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
DVPackType
DVPackType
Definition: dv.h:38
AVDVProfile::difseg_size
int difseg_size
Definition: dv_profile.h:42
avpriv_dv_produce_packet
int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, uint8_t *buf, int buf_size, int64_t pos)
Definition: dv.c:735
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
finish
static void finish(void)
Definition: movenc.c:342
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
timecode.h
DV_VIDEO_CONTROL
@ DV_VIDEO_CONTROL
Definition: dv.h:47
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
dv_profile.h
pts
static int64_t pts
Definition: transcode_aac.c:643
DV_TIMESCALE_AUDIO
#define DV_TIMESCALE_AUDIO
Definition: dv.h:67
quant
static const uint8_t quant[64]
Definition: vmixdec.c:71
avassert.h
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:760
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
duration
int64_t duration
Definition: movenc.c:64
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
DV_TIMECODE
@ DV_TIMECODE
Definition: dv.h:41
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
frame_size
int frame_size
Definition: mxfenc.c:2422
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
FFFormatContext::data_offset
int64_t data_offset
offset of the first packet
Definition: internal.h:101
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
frame
static AVFrame * frame
Definition: demux_decode.c:54
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
if
if(ret)
Definition: filter_design.txt:179
FFFormatContext
Definition: internal.h:64
DV_TIMESCALE_VIDEO
#define DV_TIMESCALE_VIDEO
Definition: dv.h:64
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
avpriv_dv_get_packet
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition: dv.c:730
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1206
AVDVProfile::n_difchan
int n_difchan
Definition: dv_profile.h:43
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
time.h
state
static struct @385 state
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:417
dv.h
AVDVProfile::audio_stride
int audio_stride
Definition: dv_profile.h:52
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
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
shift
static int shift(int a, int b)
Definition: bonk.c:262
sample
#define sample
Definition: flacdsp_template.c:44
av_dv_frame_profile
const AVDVProfile * av_dv_frame_profile(const AVDVProfile *sys, const uint8_t *frame, unsigned buf_size)
Get a DV profile for the provided compressed frame.
Definition: dv_profile.c:299
size
int size
Definition: twinvq_data.h:10344
ff_add_param_change
int ff_add_param_change(AVPacket *pkt, int32_t channels, uint64_t channel_layout, int32_t sample_rate, int32_t width, int32_t height)
Add side data to a packet for changing parameters to the given values.
Definition: demux_utils.c:151
dv_audio_12to16
static uint16_t dv_audio_12to16(uint16_t sample)
Definition: dvaudiodec.c:68
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
AVDVProfile::height
int height
Definition: dv_profile.h:46
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
DV_MAX_FRAME_SIZE
#define DV_MAX_FRAME_SIZE
largest possible DV frame, in bytes (1080i50)
Definition: dv.h:61
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:602
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
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
DV_AUDIO_SOURCE
@ DV_AUDIO_SOURCE
Definition: dv.h:42
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
av_timecode_make_smpte_tc_string2
char * av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
Get the timecode string from the SMPTE timecode format.
Definition: timecode.c:139
ff_dv_demuxer
const FFInputFormat ff_dv_demuxer
ff_dv_ts_reset
void ff_dv_ts_reset(DVDemuxContext *c, int64_t ts_video)
AV_CODEC_ID_DVVIDEO
@ AV_CODEC_ID_DVVIDEO
Definition: codec_id.h:76
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
demux.h
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
DVPacket::buf
uint8_t * buf
actual buffer data
Definition: iec61883.c:56
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
avpriv_dv_init_demux
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition: dv.c:725
AVDVProfile::audio_min_samples
int audio_min_samples[3]
Definition: dv_profile.h:53
channel_layout.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
AVDVProfile
Definition: dv_profile.h:38
AVPacket::stream_index
int stream_index
Definition: packet.h:524
tc
#define tc
Definition: regdef.h:69
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVDVProfile::audio_shuffle
const uint8_t(* audio_shuffle)[9]
Definition: dv_profile.h:57
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:542
FFInputFormat
Definition: demux.h:37
d
d
Definition: ffmpeg_filter.c:409
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVDVProfile::frame_size
int frame_size
Definition: dv_profile.h:41
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345