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.

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