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.

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