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.

514 lines
15KB

  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. size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
  110. half_ch = sys->difseg_size / 2;
  111. /* We work with 720p frames split in half, thus even frames have
  112. * channels 0,1 and odd 2,3. */
  113. ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
  114. pcm = ppcm[ipcm++];
  115. /* for each DIF channel */
  116. for (chan = 0; chan < sys->n_difchan; chan++) {
  117. /* for each DIF segment */
  118. for (i = 0; i < sys->difseg_size; i++) {
  119. frame += 6 * 80; /* skip DIF segment header */
  120. if (quant == 1 && i == half_ch) {
  121. /* next stereo channel (12bit mode only) */
  122. pcm = ppcm[ipcm++];
  123. if (!pcm)
  124. break;
  125. }
  126. /* for each AV sequence */
  127. for (j = 0; j < 9; j++) {
  128. for (d = 8; d < 80; d += 2) {
  129. if (quant == 0) { /* 16bit quantization */
  130. of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
  131. if (of*2 >= size)
  132. continue;
  133. pcm[of*2] = frame[d+1]; // FIXME: maybe we have to admit
  134. pcm[of*2+1] = frame[d]; // that DV is a big-endian PCM
  135. if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
  136. pcm[of*2+1] = 0;
  137. } else { /* 12bit quantization */
  138. lc = ((uint16_t)frame[d] << 4) |
  139. ((uint16_t)frame[d+2] >> 4);
  140. rc = ((uint16_t)frame[d+1] << 4) |
  141. ((uint16_t)frame[d+2] & 0x0f);
  142. lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
  143. rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
  144. of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
  145. if (of*2 >= size)
  146. continue;
  147. pcm[of*2] = lc & 0xff; // FIXME: maybe we have to admit
  148. pcm[of*2+1] = lc >> 8; // that DV is a big-endian PCM
  149. of = sys->audio_shuffle[i%half_ch+half_ch][j] +
  150. (d - 8) / 3 * sys->audio_stride;
  151. pcm[of*2] = rc & 0xff; // FIXME: maybe we have to admit
  152. pcm[of*2+1] = rc >> 8; // that DV is a big-endian PCM
  153. ++d;
  154. }
  155. }
  156. frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  157. }
  158. }
  159. /* next stereo channel (50Mbps and 100Mbps only) */
  160. pcm = ppcm[ipcm++];
  161. if (!pcm)
  162. break;
  163. }
  164. return size;
  165. }
  166. static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
  167. {
  168. const uint8_t* as_pack;
  169. int freq, stype, smpls, quant, i, ach;
  170. as_pack = dv_extract_pack(frame, dv_audio_source);
  171. if (!as_pack || !c->sys) { /* No audio ? */
  172. c->ach = 0;
  173. return 0;
  174. }
  175. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  176. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
  177. stype = (as_pack[3] & 0x1f); /* 0 - 2CH, 2 - 4CH, 3 - 8CH */
  178. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  179. if (stype > 3) {
  180. av_log(c->fctx, AV_LOG_ERROR, "stype %d is invalid\n", stype);
  181. c->ach = 0;
  182. return 0;
  183. }
  184. /* note: ach counts PAIRS of channels (i.e. stereo channels) */
  185. ach = ((int[4]){ 1, 0, 2, 4})[stype];
  186. if (ach == 1 && quant && freq == 2)
  187. ach = 2;
  188. /* Dynamic handling of the audio streams in DV */
  189. for (i = 0; i < ach; i++) {
  190. if (!c->ast[i]) {
  191. c->ast[i] = av_new_stream(c->fctx, 0);
  192. if (!c->ast[i])
  193. break;
  194. av_set_pts_info(c->ast[i], 64, 1, 30000);
  195. c->ast[i]->codec->codec_type = CODEC_TYPE_AUDIO;
  196. c->ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE;
  197. av_init_packet(&c->audio_pkt[i]);
  198. c->audio_pkt[i].size = 0;
  199. c->audio_pkt[i].data = c->audio_buf[i];
  200. c->audio_pkt[i].stream_index = c->ast[i]->index;
  201. c->audio_pkt[i].flags |= PKT_FLAG_KEY;
  202. }
  203. c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
  204. c->ast[i]->codec->channels = 2;
  205. c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16;
  206. c->ast[i]->start_time = 0;
  207. }
  208. c->ach = i;
  209. return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
  210. }
  211. static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
  212. {
  213. const uint8_t* vsc_pack;
  214. AVCodecContext* avctx;
  215. int apt, is16_9;
  216. int size = 0;
  217. if (c->sys) {
  218. avctx = c->vst->codec;
  219. av_set_pts_info(c->vst, 64, c->sys->time_base.num,
  220. c->sys->time_base.den);
  221. avctx->time_base= c->sys->time_base;
  222. if (!avctx->width){
  223. avctx->width = c->sys->width;
  224. avctx->height = c->sys->height;
  225. }
  226. avctx->pix_fmt = c->sys->pix_fmt;
  227. /* finding out SAR is a little bit messy */
  228. vsc_pack = dv_extract_pack(frame, dv_video_control);
  229. apt = frame[4] & 0x07;
  230. is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
  231. (!apt && (vsc_pack[2] & 0x07) == 0x07)));
  232. c->vst->sample_aspect_ratio = c->sys->sar[is16_9];
  233. avctx->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational){8,1},
  234. c->sys->time_base);
  235. size = c->sys->frame_size;
  236. }
  237. return size;
  238. }
  239. /*
  240. * The following 3 functions constitute our interface to the world
  241. */
  242. DVDemuxContext* dv_init_demux(AVFormatContext *s)
  243. {
  244. DVDemuxContext *c;
  245. c = av_mallocz(sizeof(DVDemuxContext));
  246. if (!c)
  247. return NULL;
  248. c->vst = av_new_stream(s, 0);
  249. if (!c->vst) {
  250. av_free(c);
  251. return NULL;
  252. }
  253. c->sys = NULL;
  254. c->fctx = s;
  255. memset(c->ast, 0, sizeof(c->ast));
  256. c->ach = 0;
  257. c->frames = 0;
  258. c->abytes = 0;
  259. c->vst->codec->codec_type = CODEC_TYPE_VIDEO;
  260. c->vst->codec->codec_id = CODEC_ID_DVVIDEO;
  261. c->vst->codec->bit_rate = 25000000;
  262. c->vst->start_time = 0;
  263. return c;
  264. }
  265. int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
  266. {
  267. int size = -1;
  268. int i;
  269. for (i = 0; i < c->ach; i++) {
  270. if (c->ast[i] && c->audio_pkt[i].size) {
  271. *pkt = c->audio_pkt[i];
  272. c->audio_pkt[i].size = 0;
  273. size = pkt->size;
  274. break;
  275. }
  276. }
  277. return size;
  278. }
  279. int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
  280. uint8_t* buf, int buf_size)
  281. {
  282. int size, i;
  283. uint8_t *ppcm[4] = {0};
  284. if (buf_size < DV_PROFILE_BYTES ||
  285. !(c->sys = dv_frame_profile(buf)) ||
  286. buf_size < c->sys->frame_size) {
  287. return -1; /* Broken frame, or not enough data */
  288. }
  289. /* Queueing audio packet */
  290. /* FIXME: in case of no audio/bad audio we have to do something */
  291. size = dv_extract_audio_info(c, buf);
  292. for (i = 0; i < c->ach; i++) {
  293. c->audio_pkt[i].size = size;
  294. c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
  295. ppcm[i] = c->audio_buf[i];
  296. }
  297. dv_extract_audio(buf, ppcm, c->sys);
  298. c->abytes += size;
  299. /* We work with 720p frames split in half, thus even frames have
  300. * channels 0,1 and odd 2,3. */
  301. if (c->sys->height == 720) {
  302. if (buf[1] & 0x0C)
  303. c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
  304. else
  305. c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
  306. }
  307. /* Now it's time to return video packet */
  308. size = dv_extract_video_info(c, buf);
  309. av_init_packet(pkt);
  310. pkt->data = buf;
  311. pkt->size = size;
  312. pkt->flags |= PKT_FLAG_KEY;
  313. pkt->stream_index = c->vst->id;
  314. pkt->pts = c->frames;
  315. c->frames++;
  316. return size;
  317. }
  318. static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
  319. int64_t timestamp, int flags)
  320. {
  321. // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
  322. const DVprofile* sys = dv_codec_profile(c->vst->codec);
  323. int64_t offset;
  324. int64_t size = url_fsize(s->pb);
  325. int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size;
  326. offset = sys->frame_size * timestamp;
  327. if (offset > max_offset) offset = max_offset;
  328. else if (offset < 0) offset = 0;
  329. return offset;
  330. }
  331. void dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
  332. {
  333. c->frames= frame_offset;
  334. if (c->ach)
  335. c->abytes= av_rescale_q(c->frames, c->sys->time_base,
  336. (AVRational){8, c->ast[0]->codec->bit_rate});
  337. c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
  338. c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
  339. }
  340. /************************************************************
  341. * Implementation of the easiest DV storage of all -- raw DV.
  342. ************************************************************/
  343. typedef struct RawDVContext {
  344. DVDemuxContext* dv_demux;
  345. uint8_t buf[DV_MAX_FRAME_SIZE];
  346. } RawDVContext;
  347. static int dv_read_header(AVFormatContext *s,
  348. AVFormatParameters *ap)
  349. {
  350. unsigned state;
  351. RawDVContext *c = s->priv_data;
  352. c->dv_demux = dv_init_demux(s);
  353. if (!c->dv_demux)
  354. return -1;
  355. state = get_be32(s->pb);
  356. while ((state & 0xffffff7f) != 0x1f07003f) {
  357. if (url_feof(s->pb)) {
  358. av_log(s, AV_LOG_ERROR, "Cannot find DV header.\n");
  359. return -1;
  360. }
  361. state = (state << 8) | get_byte(s->pb);
  362. }
  363. AV_WB32(c->buf, state);
  364. if (get_buffer(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) <= 0 ||
  365. url_fseek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
  366. return AVERROR(EIO);
  367. c->dv_demux->sys = dv_frame_profile(c->buf);
  368. if (!c->dv_demux->sys) {
  369. av_log(s, AV_LOG_ERROR, "Can't determine profile of DV input stream.\n");
  370. return -1;
  371. }
  372. s->bit_rate = av_rescale_q(c->dv_demux->sys->frame_size, (AVRational){8,1},
  373. c->dv_demux->sys->time_base);
  374. return 0;
  375. }
  376. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  377. {
  378. int size;
  379. RawDVContext *c = s->priv_data;
  380. size = dv_get_packet(c->dv_demux, pkt);
  381. if (size < 0) {
  382. if (!c->dv_demux->sys)
  383. return AVERROR(EIO);
  384. size = c->dv_demux->sys->frame_size;
  385. if (get_buffer(s->pb, c->buf, size) <= 0)
  386. return AVERROR(EIO);
  387. size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
  388. }
  389. return size;
  390. }
  391. static int dv_read_seek(AVFormatContext *s, int stream_index,
  392. int64_t timestamp, int flags)
  393. {
  394. RawDVContext *r = s->priv_data;
  395. DVDemuxContext *c = r->dv_demux;
  396. int64_t offset = dv_frame_offset(s, c, timestamp, flags);
  397. dv_offset_reset(c, offset / c->sys->frame_size);
  398. offset = url_fseek(s->pb, offset, SEEK_SET);
  399. return (offset < 0) ? offset : 0;
  400. }
  401. static int dv_read_close(AVFormatContext *s)
  402. {
  403. RawDVContext *c = s->priv_data;
  404. av_free(c->dv_demux);
  405. return 0;
  406. }
  407. static int dv_probe(AVProbeData *p)
  408. {
  409. unsigned state;
  410. int i;
  411. if (p->buf_size < 5)
  412. return 0;
  413. state = AV_RB32(p->buf);
  414. for (i = 4; i < p->buf_size; i++) {
  415. if ((state & 0xffffff7f) == 0x1f07003f)
  416. return AVPROBE_SCORE_MAX*3/4; // not max to avoid dv in mov to match
  417. state = (state << 8) | p->buf[i];
  418. }
  419. return 0;
  420. }
  421. #if CONFIG_DV_DEMUXER
  422. AVInputFormat dv_demuxer = {
  423. "dv",
  424. NULL_IF_CONFIG_SMALL("DV video format"),
  425. sizeof(RawDVContext),
  426. dv_probe,
  427. dv_read_header,
  428. dv_read_packet,
  429. dv_read_close,
  430. dv_read_seek,
  431. .extensions = "dv,dif",
  432. };
  433. #endif