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.

473 lines
14KB

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