dv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * General DV muxer/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. #include <time.h>
  32. #include "avformat.h"
  33. #include "internal.h"
  34. #include "libavcodec/dvdata.h"
  35. #include "libavutil/intreadwrite.h"
  36. #include "libavutil/mathematics.h"
  37. #include "dv.h"
  38. #include "libavutil/avassert.h"
  39. struct DVDemuxContext {
  40. const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */
  41. AVFormatContext* fctx;
  42. AVStream* vst;
  43. AVStream* ast[4];
  44. AVPacket audio_pkt[4];
  45. uint8_t audio_buf[4][8192];
  46. int ach;
  47. int frames;
  48. uint64_t abytes;
  49. };
  50. static inline uint16_t dv_audio_12to16(uint16_t sample)
  51. {
  52. uint16_t shift, result;
  53. sample = (sample < 0x800) ? sample : sample | 0xf000;
  54. shift = (sample & 0xf00) >> 8;
  55. if (shift < 0x2 || shift > 0xd) {
  56. result = sample;
  57. } else if (shift < 0x8) {
  58. shift--;
  59. result = (sample - (256 * shift)) << shift;
  60. } else {
  61. shift = 0xe - shift;
  62. result = ((sample + ((256 * shift) + 1)) << shift) - 1;
  63. }
  64. return result;
  65. }
  66. /*
  67. * This is the dumbest implementation of all -- it simply looks at
  68. * a fixed offset and if pack isn't there -- fails. We might want
  69. * to have a fallback mechanism for complete search of missing packs.
  70. */
  71. static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
  72. {
  73. int offs;
  74. switch (t) {
  75. case dv_audio_source:
  76. offs = (80*6 + 80*16*3 + 3);
  77. break;
  78. case dv_audio_control:
  79. offs = (80*6 + 80*16*4 + 3);
  80. break;
  81. case dv_video_control:
  82. offs = (80*5 + 48 + 5);
  83. break;
  84. case dv_timecode:
  85. offs = (80*1 + 3 + 3);
  86. break;
  87. default:
  88. return NULL;
  89. }
  90. return frame[offs] == t ? &frame[offs] : NULL;
  91. }
  92. /*
  93. * There's a couple of assumptions being made here:
  94. * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
  95. * We can pass them upwards when libavcodec will be ready to deal with them.
  96. * 2. We don't do software emphasis.
  97. * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
  98. * are converted into 16bit linear ones.
  99. */
  100. static int dv_extract_audio(uint8_t *frame, uint8_t **ppcm,
  101. const DVprofile *sys)
  102. {
  103. int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
  104. uint16_t lc, rc;
  105. const uint8_t* as_pack;
  106. uint8_t *pcm, ipcm;
  107. as_pack = dv_extract_pack(frame, dv_audio_source);
  108. if (!as_pack) /* No audio ? */
  109. return 0;
  110. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  111. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
  112. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  113. if (quant > 1)
  114. return -1; /* unsupported quantization */
  115. if (freq >= FF_ARRAY_ELEMS(dv_audio_frequency))
  116. return AVERROR_INVALIDDATA;
  117. size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
  118. half_ch = sys->difseg_size / 2;
  119. /* We work with 720p frames split in half, thus even frames have
  120. * channels 0,1 and odd 2,3. */
  121. ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
  122. /* for each DIF channel */
  123. for (chan = 0; chan < sys->n_difchan; chan++) {
  124. av_assert0(ipcm<4);
  125. /* next stereo channel (50Mbps and 100Mbps only) */
  126. pcm = ppcm[ipcm++];
  127. if (!pcm)
  128. break;
  129. /* for each DIF segment */
  130. for (i = 0; i < sys->difseg_size; i++) {
  131. frame += 6 * 80; /* skip DIF segment header */
  132. if (quant == 1 && i == half_ch) {
  133. /* next stereo channel (12bit mode only) */
  134. av_assert0(ipcm<4);
  135. pcm = ppcm[ipcm++];
  136. if (!pcm)
  137. break;
  138. }
  139. /* for each AV sequence */
  140. for (j = 0; j < 9; j++) {
  141. for (d = 8; d < 80; d += 2) {
  142. if (quant == 0) { /* 16bit quantization */
  143. of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
  144. if (of*2 >= size)
  145. continue;
  146. pcm[of*2] = frame[d+1]; // FIXME: maybe we have to admit
  147. pcm[of*2+1] = frame[d]; // that DV is a big-endian PCM
  148. if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
  149. pcm[of*2+1] = 0;
  150. } else { /* 12bit quantization */
  151. lc = ((uint16_t)frame[d] << 4) |
  152. ((uint16_t)frame[d+2] >> 4);
  153. rc = ((uint16_t)frame[d+1] << 4) |
  154. ((uint16_t)frame[d+2] & 0x0f);
  155. lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
  156. rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
  157. of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
  158. if (of*2 >= size)
  159. continue;
  160. pcm[of*2] = lc & 0xff; // FIXME: maybe we have to admit
  161. pcm[of*2+1] = lc >> 8; // that DV is a big-endian PCM
  162. of = sys->audio_shuffle[i%half_ch+half_ch][j] +
  163. (d - 8) / 3 * sys->audio_stride;
  164. pcm[of*2] = rc & 0xff; // FIXME: maybe we have to admit
  165. pcm[of*2+1] = rc >> 8; // that DV is a big-endian PCM
  166. ++d;
  167. }
  168. }
  169. frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  170. }
  171. }
  172. }
  173. return size;
  174. }
  175. static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
  176. {
  177. const uint8_t* as_pack;
  178. int freq, stype, smpls, quant, i, ach;
  179. as_pack = dv_extract_pack(frame, dv_audio_source);
  180. if (!as_pack || !c->sys) { /* No audio ? */
  181. c->ach = 0;
  182. return 0;
  183. }
  184. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  185. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
  186. stype = (as_pack[3] & 0x1f); /* 0 - 2CH, 2 - 4CH, 3 - 8CH */
  187. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  188. if (freq >= FF_ARRAY_ELEMS(dv_audio_frequency)) {
  189. av_log(c->fctx, AV_LOG_ERROR,
  190. "Unrecognized audio sample rate index (%d)\n", freq);
  191. return 0;
  192. }
  193. if (stype > 3) {
  194. av_log(c->fctx, AV_LOG_ERROR, "stype %d is invalid\n", stype);
  195. c->ach = 0;
  196. return 0;
  197. }
  198. /* note: ach counts PAIRS of channels (i.e. stereo channels) */
  199. ach = ((int[4]){ 1, 0, 2, 4})[stype];
  200. if (ach == 1 && quant && freq == 2)
  201. ach = 2;
  202. /* Dynamic handling of the audio streams in DV */
  203. for (i = 0; i < ach; i++) {
  204. if (!c->ast[i]) {
  205. c->ast[i] = avformat_new_stream(c->fctx, NULL);
  206. if (!c->ast[i])
  207. break;
  208. avpriv_set_pts_info(c->ast[i], 64, 1, 30000);
  209. c->ast[i]->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  210. c->ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE;
  211. av_init_packet(&c->audio_pkt[i]);
  212. c->audio_pkt[i].size = 0;
  213. c->audio_pkt[i].data = c->audio_buf[i];
  214. c->audio_pkt[i].stream_index = c->ast[i]->index;
  215. c->audio_pkt[i].flags |= AV_PKT_FLAG_KEY;
  216. }
  217. c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
  218. c->ast[i]->codec->channels = 2;
  219. c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16;
  220. c->ast[i]->start_time = 0;
  221. }
  222. c->ach = i;
  223. return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
  224. }
  225. static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
  226. {
  227. const uint8_t* vsc_pack;
  228. AVCodecContext* avctx;
  229. int apt, is16_9;
  230. int size = 0;
  231. if (c->sys) {
  232. avctx = c->vst->codec;
  233. avpriv_set_pts_info(c->vst, 64, c->sys->time_base.num,
  234. c->sys->time_base.den);
  235. avctx->time_base= c->sys->time_base;
  236. /* finding out SAR is a little bit messy */
  237. vsc_pack = dv_extract_pack(frame, dv_video_control);
  238. apt = frame[4] & 0x07;
  239. is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
  240. (!apt && (vsc_pack[2] & 0x07) == 0x07)));
  241. c->vst->sample_aspect_ratio = c->sys->sar[is16_9];
  242. avctx->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational){8,1},
  243. c->sys->time_base);
  244. size = c->sys->frame_size;
  245. }
  246. return size;
  247. }
  248. static int bcd2int(uint8_t bcd)
  249. {
  250. int low = bcd & 0xf;
  251. int high = bcd >> 4;
  252. if (low > 9 || high > 9)
  253. return -1;
  254. return low + 10*high;
  255. }
  256. static int dv_extract_timecode(DVDemuxContext* c, uint8_t* frame, char tc[32])
  257. {
  258. int hh, mm, ss, ff, drop_frame;
  259. const uint8_t *tc_pack;
  260. tc_pack = dv_extract_pack(frame, dv_timecode);
  261. if (!tc_pack)
  262. return 0;
  263. ff = bcd2int(tc_pack[1] & 0x3f);
  264. ss = bcd2int(tc_pack[2] & 0x7f);
  265. mm = bcd2int(tc_pack[3] & 0x7f);
  266. hh = bcd2int(tc_pack[4] & 0x3f);
  267. drop_frame = tc_pack[1] >> 6 & 0x1;
  268. if (ff < 0 || ss < 0 || mm < 0 || hh < 0)
  269. return -1;
  270. // For PAL systems, drop frame bit is replaced by an arbitrary
  271. // bit so its value should not be considered. Drop frame timecode
  272. // is only relevant for NTSC systems.
  273. if(c->sys->ltc_divisor == 25 || c->sys->ltc_divisor == 50) {
  274. drop_frame = 0;
  275. }
  276. snprintf(tc, 32, "%02d:%02d:%02d%c%02d",
  277. hh, mm, ss, drop_frame ? ';' : ':', ff);
  278. return 1;
  279. }
  280. /*
  281. * The following 3 functions constitute our interface to the world
  282. */
  283. DVDemuxContext* avpriv_dv_init_demux(AVFormatContext *s)
  284. {
  285. DVDemuxContext *c;
  286. c = av_mallocz(sizeof(DVDemuxContext));
  287. if (!c)
  288. return NULL;
  289. c->vst = avformat_new_stream(s, NULL);
  290. if (!c->vst) {
  291. av_free(c);
  292. return NULL;
  293. }
  294. c->sys = NULL;
  295. c->fctx = s;
  296. memset(c->ast, 0, sizeof(c->ast));
  297. c->ach = 0;
  298. c->frames = 0;
  299. c->abytes = 0;
  300. c->vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  301. c->vst->codec->codec_id = CODEC_ID_DVVIDEO;
  302. c->vst->codec->bit_rate = 25000000;
  303. c->vst->start_time = 0;
  304. return c;
  305. }
  306. int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
  307. {
  308. int size = -1;
  309. int i;
  310. for (i = 0; i < c->ach; i++) {
  311. if (c->ast[i] && c->audio_pkt[i].size) {
  312. *pkt = c->audio_pkt[i];
  313. c->audio_pkt[i].size = 0;
  314. size = pkt->size;
  315. break;
  316. }
  317. }
  318. return size;
  319. }
  320. int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
  321. uint8_t* buf, int buf_size, int64_t pos)
  322. {
  323. int size, i;
  324. uint8_t *ppcm[5] = { 0 };
  325. if (buf_size < DV_PROFILE_BYTES ||
  326. !(c->sys = avpriv_dv_frame_profile(c->sys, buf, buf_size)) ||
  327. buf_size < c->sys->frame_size) {
  328. return -1; /* Broken frame, or not enough data */
  329. }
  330. /* Queueing audio packet */
  331. /* FIXME: in case of no audio/bad audio we have to do something */
  332. size = dv_extract_audio_info(c, buf);
  333. for (i = 0; i < c->ach; i++) {
  334. c->audio_pkt[i].pos = pos;
  335. c->audio_pkt[i].size = size;
  336. c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
  337. ppcm[i] = c->audio_buf[i];
  338. }
  339. if (c->ach)
  340. dv_extract_audio(buf, ppcm, c->sys);
  341. /* We work with 720p frames split in half, thus even frames have
  342. * channels 0,1 and odd 2,3. */
  343. if (c->sys->height == 720) {
  344. if (buf[1] & 0x0C) {
  345. c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
  346. } else {
  347. c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
  348. c->abytes += size;
  349. }
  350. } else {
  351. c->abytes += size;
  352. }
  353. /* Now it's time to return video packet */
  354. size = dv_extract_video_info(c, buf);
  355. av_init_packet(pkt);
  356. pkt->data = buf;
  357. pkt->pos = pos;
  358. pkt->size = size;
  359. pkt->flags |= AV_PKT_FLAG_KEY;
  360. pkt->stream_index = c->vst->id;
  361. pkt->pts = c->frames;
  362. c->frames++;
  363. return size;
  364. }
  365. static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
  366. int64_t timestamp, int flags)
  367. {
  368. // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
  369. const DVprofile* sys = avpriv_dv_codec_profile(c->vst->codec);
  370. int64_t offset;
  371. int64_t size = avio_size(s->pb) - s->data_offset;
  372. int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size;
  373. offset = sys->frame_size * timestamp;
  374. if (size >= 0 && offset > max_offset) offset = max_offset;
  375. else if (offset < 0) offset = 0;
  376. return offset + s->data_offset;
  377. }
  378. void dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
  379. {
  380. c->frames= frame_offset;
  381. if (c->ach)
  382. c->abytes= av_rescale_q(c->frames, c->sys->time_base,
  383. (AVRational){8, c->ast[0]->codec->bit_rate});
  384. c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
  385. c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
  386. }
  387. /************************************************************
  388. * Implementation of the easiest DV storage of all -- raw DV.
  389. ************************************************************/
  390. typedef struct RawDVContext {
  391. DVDemuxContext* dv_demux;
  392. uint8_t buf[DV_MAX_FRAME_SIZE];
  393. } RawDVContext;
  394. static int dv_read_timecode(AVFormatContext *s) {
  395. int ret;
  396. char timecode[32];
  397. int64_t pos = avio_tell(s->pb);
  398. // Read 3 DIF blocks: Header block and 2 Subcode blocks.
  399. int partial_frame_size = 3 * 80;
  400. uint8_t *partial_frame = av_mallocz(sizeof(*partial_frame) *
  401. partial_frame_size);
  402. RawDVContext *c = s->priv_data;
  403. ret = avio_read(s->pb, partial_frame, partial_frame_size);
  404. if (ret < 0)
  405. goto finish;
  406. if (ret < partial_frame_size) {
  407. ret = -1;
  408. goto finish;
  409. }
  410. ret = dv_extract_timecode(c->dv_demux, partial_frame, timecode);
  411. if (ret)
  412. av_dict_set(&s->metadata, "timecode", timecode, 0);
  413. else if (ret < 0)
  414. av_log(s, AV_LOG_ERROR, "Detected timecode is invalid");
  415. finish:
  416. av_free(partial_frame);
  417. avio_seek(s->pb, pos, SEEK_SET);
  418. return ret;
  419. }
  420. static int dv_read_header(AVFormatContext *s,
  421. AVFormatParameters *ap)
  422. {
  423. unsigned state, marker_pos = 0;
  424. RawDVContext *c = s->priv_data;
  425. c->dv_demux = avpriv_dv_init_demux(s);
  426. if (!c->dv_demux)
  427. return -1;
  428. state = avio_rb32(s->pb);
  429. while ((state & 0xffffff7f) != 0x1f07003f) {
  430. if (url_feof(s->pb)) {
  431. av_log(s, AV_LOG_ERROR, "Cannot find DV header.\n");
  432. return -1;
  433. }
  434. if (state == 0x003f0700 || state == 0xff3f0700)
  435. marker_pos = avio_tell(s->pb);
  436. if (state == 0xff3f0701 && avio_tell(s->pb) - marker_pos == 80) {
  437. avio_seek(s->pb, -163, SEEK_CUR);
  438. state = avio_rb32(s->pb);
  439. break;
  440. }
  441. state = (state << 8) | avio_r8(s->pb);
  442. }
  443. AV_WB32(c->buf, state);
  444. if (avio_read(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) <= 0 ||
  445. avio_seek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
  446. return AVERROR(EIO);
  447. c->dv_demux->sys = avpriv_dv_frame_profile(c->dv_demux->sys, c->buf, DV_PROFILE_BYTES);
  448. if (!c->dv_demux->sys) {
  449. av_log(s, AV_LOG_ERROR, "Can't determine profile of DV input stream.\n");
  450. return -1;
  451. }
  452. s->bit_rate = av_rescale_q(c->dv_demux->sys->frame_size, (AVRational){8,1},
  453. c->dv_demux->sys->time_base);
  454. if (s->pb->seekable)
  455. dv_read_timecode(s);
  456. return 0;
  457. }
  458. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  459. {
  460. int size;
  461. RawDVContext *c = s->priv_data;
  462. size = avpriv_dv_get_packet(c->dv_demux, pkt);
  463. if (size < 0) {
  464. int64_t pos = avio_tell(s->pb);
  465. if (!c->dv_demux->sys)
  466. return AVERROR(EIO);
  467. size = c->dv_demux->sys->frame_size;
  468. if (avio_read(s->pb, c->buf, size) <= 0)
  469. return AVERROR(EIO);
  470. size = avpriv_dv_produce_packet(c->dv_demux, pkt, c->buf, size, pos);
  471. }
  472. return size;
  473. }
  474. static int dv_read_seek(AVFormatContext *s, int stream_index,
  475. int64_t timestamp, int flags)
  476. {
  477. RawDVContext *r = s->priv_data;
  478. DVDemuxContext *c = r->dv_demux;
  479. int64_t offset = dv_frame_offset(s, c, timestamp, flags);
  480. if (avio_seek(s->pb, offset, SEEK_SET) < 0)
  481. return -1;
  482. dv_offset_reset(c, offset / c->sys->frame_size);
  483. return 0;
  484. }
  485. static int dv_read_close(AVFormatContext *s)
  486. {
  487. RawDVContext *c = s->priv_data;
  488. av_free(c->dv_demux);
  489. return 0;
  490. }
  491. static int dv_probe(AVProbeData *p)
  492. {
  493. unsigned state, marker_pos = 0;
  494. int i;
  495. int matches = 0;
  496. int secondary_matches = 0;
  497. if (p->buf_size < 5)
  498. return 0;
  499. state = AV_RB32(p->buf);
  500. for (i = 4; i < p->buf_size; i++) {
  501. if ((state & 0xffffff7f) == 0x1f07003f)
  502. matches++;
  503. // any section header, also with seq/chan num != 0,
  504. // should appear around every 12000 bytes, at least 10 per frame
  505. if ((state & 0xff07ff7f) == 0x1f07003f)
  506. secondary_matches++;
  507. if (state == 0x003f0700 || state == 0xff3f0700)
  508. marker_pos = i;
  509. if (state == 0xff3f0701 && i - marker_pos == 80)
  510. matches++;
  511. state = (state << 8) | p->buf[i];
  512. }
  513. if (matches && p->buf_size / matches < 1024*1024) {
  514. if (matches > 4 || (secondary_matches >= 10 && p->buf_size / secondary_matches < 24000))
  515. return AVPROBE_SCORE_MAX*3/4; // not max to avoid dv in mov to match
  516. return AVPROBE_SCORE_MAX/4;
  517. }
  518. return 0;
  519. }
  520. #if CONFIG_DV_DEMUXER
  521. AVInputFormat ff_dv_demuxer = {
  522. .name = "dv",
  523. .long_name = NULL_IF_CONFIG_SMALL("DV video format"),
  524. .priv_data_size = sizeof(RawDVContext),
  525. .read_probe = dv_probe,
  526. .read_header = dv_read_header,
  527. .read_packet = dv_read_packet,
  528. .read_close = dv_read_close,
  529. .read_seek = dv_read_seek,
  530. .extensions = "dv,dif",
  531. };
  532. #endif