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.

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