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.

827 lines
28KB

  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. struct DVMuxContext {
  44. const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */
  45. int n_ast; /* Number of stereo audio streams (up to 2) */
  46. AVStream *ast[2]; /* Stereo audio streams */
  47. FifoBuffer audio_data[2]; /* Fifo for storing excessive amounts of PCM */
  48. int frames; /* Number of a current frame */
  49. time_t start_time; /* Start time of recording */
  50. int has_audio; /* frame under contruction has audio */
  51. int has_video; /* frame under contruction has video */
  52. uint8_t frame_buf[DV_MAX_FRAME_SIZE]; /* frame under contruction */
  53. };
  54. static const int dv_aaux_packs_dist[12][9] = {
  55. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  56. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  57. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  58. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  59. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  60. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  61. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  62. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  63. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  64. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  65. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  66. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  67. };
  68. static inline uint16_t dv_audio_12to16(uint16_t sample)
  69. {
  70. uint16_t shift, result;
  71. sample = (sample < 0x800) ? sample : sample | 0xf000;
  72. shift = (sample & 0xf00) >> 8;
  73. if (shift < 0x2 || shift > 0xd) {
  74. result = sample;
  75. } else if (shift < 0x8) {
  76. shift--;
  77. result = (sample - (256 * shift)) << shift;
  78. } else {
  79. shift = 0xe - shift;
  80. result = ((sample + ((256 * shift) + 1)) << shift) - 1;
  81. }
  82. return result;
  83. }
  84. static int dv_audio_frame_size(const DVprofile* sys, int frame)
  85. {
  86. return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist)/
  87. sizeof(sys->audio_samples_dist[0]))];
  88. }
  89. static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf)
  90. {
  91. struct tm tc;
  92. time_t ct;
  93. int ltc_frame;
  94. /* Its hard to tell what SMPTE requires w.r.t. APT, but Quicktime needs it.
  95. * We set it based on pix_fmt value but it really should be per DV profile */
  96. int apt = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 1 : 0);
  97. buf[0] = (uint8_t)pack_id;
  98. switch (pack_id) {
  99. case dv_timecode:
  100. ct = (time_t)(c->frames / ((float)c->sys->frame_rate /
  101. (float)c->sys->frame_rate_base));
  102. brktimegm(ct, &tc);
  103. /*
  104. * LTC drop-frame frame counter drops two frames (0 and 1) every
  105. * minute, unless it is exactly divisible by 10
  106. */
  107. ltc_frame = (c->frames + 2*ct/60 - 2*ct/600) % c->sys->ltc_divisor;
  108. buf[1] = (0 << 7) | /* Color fame: 0 - unsync; 1 - sync mode */
  109. (1 << 6) | /* Drop frame timecode: 0 - nondrop; 1 - drop */
  110. ((ltc_frame / 10) << 4) | /* Tens of frames */
  111. (ltc_frame % 10); /* Units of frames */
  112. buf[2] = (1 << 7) | /* Biphase mark polarity correction: 0 - even; 1 - odd */
  113. ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
  114. (tc.tm_sec % 10); /* Units of seconds */
  115. buf[3] = (1 << 7) | /* Binary group flag BGF0 */
  116. ((tc.tm_min / 10) << 4) | /* Tens of minutes */
  117. (tc.tm_min % 10); /* Units of minutes */
  118. buf[4] = (1 << 7) | /* Binary group flag BGF2 */
  119. (1 << 6) | /* Binary group flag BGF1 */
  120. ((tc.tm_hour / 10) << 4) | /* Tens of hours */
  121. (tc.tm_hour % 10); /* Units of hours */
  122. break;
  123. case dv_audio_source: /* AAUX source pack */
  124. buf[1] = (0 << 7) | /* locked mode */
  125. (1 << 6) | /* reserved -- always 1 */
  126. (dv_audio_frame_size(c->sys, c->frames) -
  127. c->sys->audio_min_samples[0]);
  128. /* # of samples */
  129. buf[2] = (0 << 7) | /* multi-stereo */
  130. (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
  131. (0 << 4) | /* pair bit: 0 -- one pair of channels */
  132. 0; /* audio mode */
  133. buf[3] = (1 << 7) | /* res */
  134. (1 << 6) | /* multi-language flag */
  135. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  136. (apt << 1);/* definition: 0 -- 25Mbps, 2 -- 50Mbps */
  137. buf[4] = (1 << 7) | /* emphasis: 1 -- off */
  138. (0 << 6) | /* emphasis time constant: 0 -- reserved */
  139. (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */
  140. 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
  141. break;
  142. case dv_audio_control:
  143. buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
  144. (1 << 4) | /* input source: 1 -- digital input */
  145. (3 << 2) | /* compression: 3 -- no information */
  146. 0; /* misc. info/SMPTE emphasis off */
  147. buf[2] = (1 << 7) | /* recording start point: 1 -- no */
  148. (1 << 6) | /* recording end point: 1 -- no */
  149. (1 << 3) | /* recording mode: 1 -- original */
  150. 7;
  151. buf[3] = (1 << 7) | /* direction: 1 -- forward */
  152. 0x20; /* speed */
  153. buf[4] = (1 << 7) | /* reserved -- always 1 */
  154. 0x7f; /* genre category */
  155. break;
  156. case dv_audio_recdate:
  157. case dv_video_recdate: /* VAUX recording date */
  158. ct = c->start_time + (time_t)(c->frames /
  159. ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
  160. brktimegm(ct, &tc);
  161. buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
  162. /* 0xff is very likely to be "unknown" */
  163. buf[2] = (3 << 6) | /* reserved -- always 1 */
  164. ((tc.tm_mday / 10) << 4) | /* Tens of day */
  165. (tc.tm_mday % 10); /* Units of day */
  166. buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
  167. ((tc.tm_mon / 10) << 4) | /* Tens of month */
  168. (tc.tm_mon % 10); /* Units of month */
  169. buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
  170. (tc.tm_year % 10); /* Units of year */
  171. break;
  172. case dv_audio_rectime: /* AAUX recording time */
  173. case dv_video_rectime: /* VAUX recording time */
  174. ct = c->start_time + (time_t)(c->frames /
  175. ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
  176. brktimegm(ct, &tc);
  177. buf[1] = (3 << 6) | /* reserved -- always 1 */
  178. 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
  179. buf[2] = (1 << 7) | /* reserved -- always 1 */
  180. ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
  181. (tc.tm_sec % 10); /* Units of seconds */
  182. buf[3] = (1 << 7) | /* reserved -- always 1 */
  183. ((tc.tm_min / 10) << 4) | /* Tens of minutes */
  184. (tc.tm_min % 10); /* Units of minutes */
  185. buf[4] = (3 << 6) | /* reserved -- always 1 */
  186. ((tc.tm_hour / 10) << 4) | /* Tens of hours */
  187. (tc.tm_hour % 10); /* Units of hours */
  188. break;
  189. default:
  190. buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
  191. }
  192. return 5;
  193. }
  194. static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr)
  195. {
  196. int i, j, d, of, size;
  197. size = 4 * dv_audio_frame_size(c->sys, c->frames);
  198. frame_ptr += channel * c->sys->difseg_size * 150 * 80;
  199. for (i = 0; i < c->sys->difseg_size; i++) {
  200. frame_ptr += 6 * 80; /* skip DIF segment header */
  201. for (j = 0; j < 9; j++) {
  202. dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3]);
  203. for (d = 8; d < 80; d+=2) {
  204. of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
  205. if (of*2 >= size)
  206. continue;
  207. frame_ptr[d] = fifo_peek(&c->audio_data[channel], of*2+1); // FIXME: may be we have to admit
  208. frame_ptr[d+1] = fifo_peek(&c->audio_data[channel], of*2); // that DV is a big endian PCM
  209. }
  210. frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  211. }
  212. }
  213. }
  214. static void dv_inject_metadata(DVMuxContext *c, uint8_t* frame)
  215. {
  216. int j, k;
  217. uint8_t* buf;
  218. for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80) {
  219. /* DV subcode: 2nd and 3d DIFs */
  220. for (j = 80; j < 80 * 3; j += 80) {
  221. for (k = 6; k < 6 * 8; k += 8)
  222. dv_write_pack(dv_timecode, c, &buf[j+k]);
  223. if (((long)(buf-frame)/(c->sys->frame_size/(c->sys->difseg_size*c->sys->n_difchan))%c->sys->difseg_size) > 5) { /* FIXME: is this really needed ? */
  224. dv_write_pack(dv_video_recdate, c, &buf[j+14]);
  225. dv_write_pack(dv_video_rectime, c, &buf[j+22]);
  226. dv_write_pack(dv_video_recdate, c, &buf[j+38]);
  227. dv_write_pack(dv_video_rectime, c, &buf[j+46]);
  228. }
  229. }
  230. /* DV VAUX: 4th, 5th and 6th 3DIFs */
  231. for (j = 80*3 + 3; j < 80*6; j += 80) {
  232. dv_write_pack(dv_video_recdate, c, &buf[j+5*2]);
  233. dv_write_pack(dv_video_rectime, c, &buf[j+5*3]);
  234. dv_write_pack(dv_video_recdate, c, &buf[j+5*11]);
  235. dv_write_pack(dv_video_rectime, c, &buf[j+5*12]);
  236. }
  237. }
  238. }
  239. /*
  240. * This is the dumbest implementation of all -- it simply looks at
  241. * a fixed offset and if pack isn't there -- fails. We might want
  242. * to have a fallback mechanism for complete search of missing packs.
  243. */
  244. static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
  245. {
  246. int offs;
  247. switch (t) {
  248. case dv_audio_source:
  249. offs = (80*6 + 80*16*3 + 3);
  250. break;
  251. case dv_audio_control:
  252. offs = (80*6 + 80*16*4 + 3);
  253. break;
  254. case dv_video_control:
  255. offs = (80*5 + 48 + 5);
  256. break;
  257. default:
  258. return NULL;
  259. }
  260. return (frame[offs] == t ? &frame[offs] : NULL);
  261. }
  262. /*
  263. * There's a couple of assumptions being made here:
  264. * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
  265. * We can pass them upwards when ffmpeg will be ready to deal with them.
  266. * 2. We don't do software emphasis.
  267. * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
  268. * are converted into 16bit linear ones.
  269. */
  270. static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2,
  271. const DVprofile *sys)
  272. {
  273. int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
  274. uint16_t lc, rc;
  275. const uint8_t* as_pack;
  276. as_pack = dv_extract_pack(frame, dv_audio_source);
  277. if (!as_pack) /* No audio ? */
  278. return 0;
  279. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  280. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  281. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  282. if (quant > 1)
  283. return -1; /* Unsupported quantization */
  284. size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
  285. half_ch = sys->difseg_size/2;
  286. /* for each DIF channel */
  287. for (chan = 0; chan < sys->n_difchan; chan++) {
  288. /* for each DIF segment */
  289. for (i = 0; i < sys->difseg_size; i++) {
  290. frame += 6 * 80; /* skip DIF segment header */
  291. if (quant == 1 && i == half_ch) {
  292. /* next stereo channel (12bit mode only) */
  293. if (!pcm2)
  294. break;
  295. else
  296. pcm = pcm2;
  297. }
  298. /* for each AV sequence */
  299. for (j = 0; j < 9; j++) {
  300. for (d = 8; d < 80; d += 2) {
  301. if (quant == 0) { /* 16bit quantization */
  302. of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride;
  303. if (of*2 >= size)
  304. continue;
  305. pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit
  306. pcm[of*2+1] = frame[d]; // that DV is a big endian PCM
  307. if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
  308. pcm[of*2+1] = 0;
  309. } else { /* 12bit quantization */
  310. lc = ((uint16_t)frame[d] << 4) |
  311. ((uint16_t)frame[d+2] >> 4);
  312. rc = ((uint16_t)frame[d+1] << 4) |
  313. ((uint16_t)frame[d+2] & 0x0f);
  314. lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
  315. rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
  316. of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride;
  317. if (of*2 >= size)
  318. continue;
  319. pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit
  320. pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM
  321. of = sys->audio_shuffle[i%half_ch+half_ch][j] +
  322. (d - 8)/3 * sys->audio_stride;
  323. pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit
  324. pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM
  325. ++d;
  326. }
  327. }
  328. frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  329. }
  330. }
  331. /* next stereo channel (50Mbps only) */
  332. if(!pcm2)
  333. break;
  334. pcm = pcm2;
  335. }
  336. return size;
  337. }
  338. static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
  339. {
  340. const uint8_t* as_pack;
  341. int freq, stype, smpls, quant, i, ach;
  342. as_pack = dv_extract_pack(frame, dv_audio_source);
  343. if (!as_pack || !c->sys) { /* No audio ? */
  344. c->ach = 0;
  345. return 0;
  346. }
  347. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  348. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  349. stype = (as_pack[3] & 0x1f); /* 0 - 2CH, 2 - 4CH */
  350. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  351. /* note: ach counts PAIRS of channels (i.e. stereo channels) */
  352. ach = (stype == 2 || (quant && (freq == 2))) ? 2 : 1;
  353. /* Dynamic handling of the audio streams in DV */
  354. for (i=0; i<ach; i++) {
  355. if (!c->ast[i]) {
  356. c->ast[i] = av_new_stream(c->fctx, 0);
  357. if (!c->ast[i])
  358. break;
  359. av_set_pts_info(c->ast[i], 64, 1, 30000);
  360. c->ast[i]->codec->codec_type = CODEC_TYPE_AUDIO;
  361. c->ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE;
  362. av_init_packet(&c->audio_pkt[i]);
  363. c->audio_pkt[i].size = 0;
  364. c->audio_pkt[i].data = c->audio_buf[i];
  365. c->audio_pkt[i].stream_index = c->ast[i]->index;
  366. c->audio_pkt[i].flags |= PKT_FLAG_KEY;
  367. }
  368. c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
  369. c->ast[i]->codec->channels = 2;
  370. c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16;
  371. c->ast[i]->start_time = 0;
  372. }
  373. c->ach = i;
  374. return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
  375. }
  376. static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
  377. {
  378. const uint8_t* vsc_pack;
  379. AVCodecContext* avctx;
  380. int apt, is16_9;
  381. int size = 0;
  382. if (c->sys) {
  383. avctx = c->vst->codec;
  384. av_set_pts_info(c->vst, 64, c->sys->frame_rate_base, c->sys->frame_rate);
  385. avctx->time_base= (AVRational){c->sys->frame_rate_base, c->sys->frame_rate};
  386. if(!avctx->width){
  387. avctx->width = c->sys->width;
  388. avctx->height = c->sys->height;
  389. }
  390. avctx->pix_fmt = c->sys->pix_fmt;
  391. /* finding out SAR is a little bit messy */
  392. vsc_pack = dv_extract_pack(frame, dv_video_control);
  393. apt = frame[4] & 0x07;
  394. is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
  395. (!apt && (vsc_pack[2] & 0x07) == 0x07)));
  396. avctx->sample_aspect_ratio = c->sys->sar[is16_9];
  397. avctx->bit_rate = av_rescale(c->sys->frame_size * 8,
  398. c->sys->frame_rate,
  399. c->sys->frame_rate_base);
  400. size = c->sys->frame_size;
  401. }
  402. return size;
  403. }
  404. /*
  405. * The following 6 functions constitute our interface to the world
  406. */
  407. int dv_assemble_frame(DVMuxContext *c, AVStream* st,
  408. const uint8_t* data, int data_size, uint8_t** frame)
  409. {
  410. int i, reqasize;
  411. *frame = &c->frame_buf[0];
  412. reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
  413. switch (st->codec->codec_type) {
  414. case CODEC_TYPE_VIDEO:
  415. /* FIXME: we have to have more sensible approach than this one */
  416. if (c->has_video)
  417. av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
  418. memcpy(*frame, data, c->sys->frame_size);
  419. c->has_video = 1;
  420. break;
  421. case CODEC_TYPE_AUDIO:
  422. for (i = 0; i < c->n_ast && st != c->ast[i]; i++);
  423. /* FIXME: we have to have more sensible approach than this one */
  424. if (fifo_size(&c->audio_data[i], c->audio_data[i].rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
  425. av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
  426. fifo_write(&c->audio_data[i], data, data_size, &c->audio_data[i].wptr);
  427. /* Lets see if we've got enough audio for one DV frame */
  428. c->has_audio |= ((reqasize <= fifo_size(&c->audio_data[i], c->audio_data[i].rptr)) << i);
  429. break;
  430. default:
  431. break;
  432. }
  433. /* Lets see if we have enough data to construct one DV frame */
  434. if (c->has_video == 1 && c->has_audio + 1 == 1<<c->n_ast) {
  435. dv_inject_metadata(c, *frame);
  436. for (i=0; i<c->n_ast; i++) {
  437. dv_inject_audio(c, i, *frame);
  438. fifo_drain(&c->audio_data[i], reqasize);
  439. }
  440. c->has_video = 0;
  441. c->has_audio = 0;
  442. c->frames++;
  443. return c->sys->frame_size;
  444. }
  445. return 0;
  446. }
  447. DVMuxContext* dv_init_mux(AVFormatContext* s)
  448. {
  449. DVMuxContext *c;
  450. AVStream *vst = NULL;
  451. int i;
  452. /* we support at most 1 video and 2 audio streams */
  453. if (s->nb_streams > 3)
  454. return NULL;
  455. c = av_mallocz(sizeof(DVMuxContext));
  456. if (!c)
  457. return NULL;
  458. c->n_ast = 0;
  459. c->ast[0] = c->ast[1] = NULL;
  460. /* We have to sort out where audio and where video stream is */
  461. for (i=0; i<s->nb_streams; i++) {
  462. switch (s->streams[i]->codec->codec_type) {
  463. case CODEC_TYPE_VIDEO:
  464. vst = s->streams[i];
  465. break;
  466. case CODEC_TYPE_AUDIO:
  467. c->ast[c->n_ast++] = s->streams[i];
  468. break;
  469. default:
  470. goto bail_out;
  471. }
  472. }
  473. /* Some checks -- DV format is very picky about its incoming streams */
  474. if (!vst || vst->codec->codec_id != CODEC_ID_DVVIDEO)
  475. goto bail_out;
  476. for (i=0; i<c->n_ast; i++) {
  477. if (c->ast[i] && (c->ast[i]->codec->codec_id != CODEC_ID_PCM_S16LE ||
  478. c->ast[i]->codec->sample_rate != 48000 ||
  479. c->ast[i]->codec->channels != 2))
  480. goto bail_out;
  481. }
  482. c->sys = dv_codec_profile(vst->codec);
  483. if (!c->sys)
  484. goto bail_out;
  485. if((c->n_ast > 1) && (c->sys->n_difchan < 2)) {
  486. /* only 1 stereo pair is allowed in 25Mbps mode */
  487. goto bail_out;
  488. }
  489. /* Ok, everything seems to be in working order */
  490. c->frames = 0;
  491. c->has_audio = 0;
  492. c->has_video = 0;
  493. c->start_time = (time_t)s->timestamp;
  494. for (i=0; i<c->n_ast; i++) {
  495. if (c->ast[i] && fifo_init(&c->audio_data[i], 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) {
  496. while (i>0) {
  497. i--;
  498. fifo_free(&c->audio_data[i]);
  499. }
  500. goto bail_out;
  501. }
  502. }
  503. return c;
  504. bail_out:
  505. av_free(c);
  506. return NULL;
  507. }
  508. void dv_delete_mux(DVMuxContext *c)
  509. {
  510. int i;
  511. for (i=0; i < c->n_ast; i++)
  512. fifo_free(&c->audio_data[i]);
  513. }
  514. DVDemuxContext* dv_init_demux(AVFormatContext *s)
  515. {
  516. DVDemuxContext *c;
  517. c = av_mallocz(sizeof(DVDemuxContext));
  518. if (!c)
  519. return NULL;
  520. c->vst = av_new_stream(s, 0);
  521. if (!c->vst) {
  522. av_free(c);
  523. return NULL;
  524. }
  525. c->sys = NULL;
  526. c->fctx = s;
  527. c->ast[0] = c->ast[1] = NULL;
  528. c->ach = 0;
  529. c->frames = 0;
  530. c->abytes = 0;
  531. c->vst->codec->codec_type = CODEC_TYPE_VIDEO;
  532. c->vst->codec->codec_id = CODEC_ID_DVVIDEO;
  533. c->vst->codec->bit_rate = 25000000;
  534. c->vst->start_time = 0;
  535. return c;
  536. }
  537. int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
  538. {
  539. int size = -1;
  540. int i;
  541. for (i=0; i<c->ach; i++) {
  542. if (c->ast[i] && c->audio_pkt[i].size) {
  543. *pkt = c->audio_pkt[i];
  544. c->audio_pkt[i].size = 0;
  545. size = pkt->size;
  546. break;
  547. }
  548. }
  549. return size;
  550. }
  551. int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
  552. uint8_t* buf, int buf_size)
  553. {
  554. int size, i;
  555. if (buf_size < DV_PROFILE_BYTES ||
  556. !(c->sys = dv_frame_profile(buf)) ||
  557. buf_size < c->sys->frame_size) {
  558. return -1; /* Broken frame, or not enough data */
  559. }
  560. /* Queueing audio packet */
  561. /* FIXME: in case of no audio/bad audio we have to do something */
  562. size = dv_extract_audio_info(c, buf);
  563. for (i=0; i<c->ach; i++) {
  564. c->audio_pkt[i].size = size;
  565. c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
  566. }
  567. dv_extract_audio(buf, c->audio_buf[0], c->audio_buf[1], c->sys);
  568. c->abytes += size;
  569. /* Now it's time to return video packet */
  570. size = dv_extract_video_info(c, buf);
  571. av_init_packet(pkt);
  572. pkt->data = buf;
  573. pkt->size = size;
  574. pkt->flags |= PKT_FLAG_KEY;
  575. pkt->stream_index = c->vst->id;
  576. pkt->pts = c->frames;
  577. c->frames++;
  578. return size;
  579. }
  580. static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
  581. int64_t timestamp, int flags)
  582. {
  583. // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
  584. const DVprofile* sys = dv_codec_profile(c->vst->codec);
  585. int64_t offset;
  586. int64_t size = url_fsize(&s->pb);
  587. int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size;
  588. offset = sys->frame_size * timestamp;
  589. if (offset > max_offset) offset = max_offset;
  590. else if (offset < 0) offset = 0;
  591. return offset;
  592. }
  593. void dv_flush_audio_packets(DVDemuxContext *c)
  594. {
  595. c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
  596. }
  597. /************************************************************
  598. * Implementation of the easiest DV storage of all -- raw DV.
  599. ************************************************************/
  600. typedef struct RawDVContext {
  601. DVDemuxContext* dv_demux;
  602. uint8_t buf[DV_MAX_FRAME_SIZE];
  603. } RawDVContext;
  604. static int dv_read_header(AVFormatContext *s,
  605. AVFormatParameters *ap)
  606. {
  607. RawDVContext *c = s->priv_data;
  608. c->dv_demux = dv_init_demux(s);
  609. if (!c->dv_demux)
  610. return -1;
  611. if (get_buffer(&s->pb, c->buf, DV_PROFILE_BYTES) <= 0 ||
  612. url_fseek(&s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
  613. return AVERROR_IO;
  614. c->dv_demux->sys = dv_frame_profile(c->buf);
  615. s->bit_rate = av_rescale(c->dv_demux->sys->frame_size * 8,
  616. c->dv_demux->sys->frame_rate,
  617. c->dv_demux->sys->frame_rate_base);
  618. return 0;
  619. }
  620. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  621. {
  622. int size;
  623. RawDVContext *c = s->priv_data;
  624. size = dv_get_packet(c->dv_demux, pkt);
  625. if (size < 0) {
  626. size = c->dv_demux->sys->frame_size;
  627. if (get_buffer(&s->pb, c->buf, size) <= 0)
  628. return AVERROR_IO;
  629. size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
  630. }
  631. return size;
  632. }
  633. static int dv_read_seek(AVFormatContext *s, int stream_index,
  634. int64_t timestamp, int flags)
  635. {
  636. RawDVContext *r = s->priv_data;
  637. DVDemuxContext *c = r->dv_demux;
  638. int64_t offset= dv_frame_offset(s, c, timestamp, flags);
  639. c->frames= offset / c->sys->frame_size;
  640. if (c->ach)
  641. c->abytes= av_rescale(c->frames,
  642. c->ast[0]->codec->bit_rate * (int64_t)c->sys->frame_rate_base,
  643. 8*c->sys->frame_rate);
  644. dv_flush_audio_packets(c);
  645. return url_fseek(&s->pb, offset, SEEK_SET);
  646. }
  647. static int dv_read_close(AVFormatContext *s)
  648. {
  649. RawDVContext *c = s->priv_data;
  650. av_free(c->dv_demux);
  651. return 0;
  652. }
  653. #ifdef CONFIG_MUXERS
  654. static int dv_write_header(AVFormatContext *s)
  655. {
  656. s->priv_data = dv_init_mux(s);
  657. if (!s->priv_data) {
  658. av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
  659. "Make sure that you supply exactly two streams:\n"
  660. " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n"
  661. " (50Mbps allows an optional second audio stream)\n");
  662. return -1;
  663. }
  664. return 0;
  665. }
  666. static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  667. {
  668. uint8_t* frame;
  669. int fsize;
  670. fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index],
  671. pkt->data, pkt->size, &frame);
  672. if (fsize > 0) {
  673. put_buffer(&s->pb, frame, fsize);
  674. put_flush_packet(&s->pb);
  675. }
  676. return 0;
  677. }
  678. /*
  679. * We might end up with some extra A/V data without matching counterpart.
  680. * E.g. video data without enough audio to write the complete frame.
  681. * Currently we simply drop the last frame. I don't know whether this
  682. * is the best strategy of all
  683. */
  684. static int dv_write_trailer(struct AVFormatContext *s)
  685. {
  686. dv_delete_mux((DVMuxContext *)s->priv_data);
  687. return 0;
  688. }
  689. #endif /* CONFIG_MUXERS */
  690. #ifdef CONFIG_DV_DEMUXER
  691. AVInputFormat dv_demuxer = {
  692. "dv",
  693. "DV video format",
  694. sizeof(RawDVContext),
  695. NULL,
  696. dv_read_header,
  697. dv_read_packet,
  698. dv_read_close,
  699. dv_read_seek,
  700. .extensions = "dv,dif",
  701. };
  702. #endif
  703. #ifdef CONFIG_DV_MUXER
  704. AVOutputFormat dv_muxer = {
  705. "dv",
  706. "DV video format",
  707. NULL,
  708. "dv",
  709. sizeof(DVMuxContext),
  710. CODEC_ID_PCM_S16LE,
  711. CODEC_ID_DVVIDEO,
  712. dv_write_header,
  713. dv_write_packet,
  714. dv_write_trailer,
  715. };
  716. #endif