You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

534 lines
17KB

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