dv.c 17 KB

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