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.

1035 lines
37KB

  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. uint8_t aspect; /* Aspect ID 0 - 4:3, 7 - 16:9 */
  51. int has_audio; /* frame under contruction has audio */
  52. int has_video; /* frame under contruction has video */
  53. uint8_t frame_buf[DV_MAX_FRAME_SIZE]; /* frame under contruction */
  54. };
  55. enum dv_section_type {
  56. dv_sect_header = 0x1f,
  57. dv_sect_subcode = 0x3f,
  58. dv_sect_vaux = 0x56,
  59. dv_sect_audio = 0x76,
  60. dv_sect_video = 0x96,
  61. };
  62. enum dv_pack_type {
  63. dv_header525 = 0x3f, /* see dv_write_pack for important details on */
  64. dv_header625 = 0xbf, /* these two packs */
  65. dv_timecode = 0x13,
  66. dv_audio_source = 0x50,
  67. dv_audio_control = 0x51,
  68. dv_audio_recdate = 0x52,
  69. dv_audio_rectime = 0x53,
  70. dv_video_source = 0x60,
  71. dv_video_control = 0x61,
  72. dv_video_recdate = 0x62,
  73. dv_video_rectime = 0x63,
  74. dv_unknown_pack = 0xff,
  75. };
  76. /*
  77. * The reason why the following three big ugly looking tables are
  78. * here is my lack of DV spec IEC 61834. The tables were basically
  79. * constructed to make code that places packs in SSYB, VAUX and
  80. * AAUX blocks very simple and table-driven. They conform to the
  81. * SMPTE 314M and the output of my personal DV camcorder, neither
  82. * of which is sufficient for a reliable DV stream producing. Thus
  83. * while code is still in development I'll be gathering input from
  84. * people with different DV equipment and modifying the tables to
  85. * accommodate all the quirks. Later on, if possible, some of them
  86. * will be folded into smaller tables and/or switch-if logic. For
  87. * now, my only excuse is -- they don't eat up that much of a space.
  88. */
  89. static const int dv_ssyb_packs_dist[12][6] = {
  90. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  91. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  92. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  93. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  94. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  95. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  96. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  97. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  98. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  99. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  100. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  101. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  102. };
  103. static const int dv_vaux_packs_dist[12][15] = {
  104. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  105. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  106. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  107. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  108. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  109. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  110. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  111. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  112. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  113. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  114. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  115. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  116. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  117. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  118. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  119. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  120. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  121. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  122. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  123. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  124. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  125. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  126. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  127. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  128. };
  129. static const int dv_aaux_packs_dist[12][9] = {
  130. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  131. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  132. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  133. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  134. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  135. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  136. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  137. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  138. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  139. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  140. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  141. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  142. };
  143. static inline uint16_t dv_audio_12to16(uint16_t sample)
  144. {
  145. uint16_t shift, result;
  146. sample = (sample < 0x800) ? sample : sample | 0xf000;
  147. shift = (sample & 0xf00) >> 8;
  148. if (shift < 0x2 || shift > 0xd) {
  149. result = sample;
  150. } else if (shift < 0x8) {
  151. shift--;
  152. result = (sample - (256 * shift)) << shift;
  153. } else {
  154. shift = 0xe - shift;
  155. result = ((sample + ((256 * shift) + 1)) << shift) - 1;
  156. }
  157. return result;
  158. }
  159. static int dv_audio_frame_size(const DVprofile* sys, int frame)
  160. {
  161. return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist)/
  162. sizeof(sys->audio_samples_dist[0]))];
  163. }
  164. static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf)
  165. {
  166. struct tm tc;
  167. time_t ct;
  168. int ltc_frame;
  169. /* Its hard to tell what SMPTE requires w.r.t. APT, but Quicktime needs it.
  170. * We set it based on pix_fmt value but it really should be per DV profile */
  171. int apt = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 1 : 0);
  172. buf[0] = (uint8_t)pack_id;
  173. switch (pack_id) {
  174. case dv_header525: /* I can't imagine why these two weren't defined as real */
  175. case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
  176. buf[1] = 0xf8 | /* reserved -- always 1 */
  177. (apt & 0x07); /* APT: Track application ID */
  178. buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
  179. (0x0f << 3) | /* reserved -- always 1 */
  180. (apt & 0x07); /* AP1: Audio application ID */
  181. buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
  182. (0x0f << 3) | /* reserved -- always 1 */
  183. (apt & 0x07); /* AP2: Video application ID */
  184. buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
  185. (0x0f << 3) | /* reserved -- always 1 */
  186. (apt & 0x07); /* AP3: Subcode application ID */
  187. break;
  188. case dv_timecode:
  189. ct = (time_t)(c->frames / ((float)c->sys->frame_rate /
  190. (float)c->sys->frame_rate_base));
  191. brktimegm(ct, &tc);
  192. /*
  193. * LTC drop-frame frame counter drops two frames (0 and 1) every
  194. * minute, unless it is exactly divisible by 10
  195. */
  196. ltc_frame = (c->frames + 2*ct/60 - 2*ct/600) % c->sys->ltc_divisor;
  197. buf[1] = (0 << 7) | /* Color fame: 0 - unsync; 1 - sync mode */
  198. (1 << 6) | /* Drop frame timecode: 0 - nondrop; 1 - drop */
  199. ((ltc_frame / 10) << 4) | /* Tens of frames */
  200. (ltc_frame % 10); /* Units of frames */
  201. buf[2] = (1 << 7) | /* Biphase mark polarity correction: 0 - even; 1 - odd */
  202. ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
  203. (tc.tm_sec % 10); /* Units of seconds */
  204. buf[3] = (1 << 7) | /* Binary group flag BGF0 */
  205. ((tc.tm_min / 10) << 4) | /* Tens of minutes */
  206. (tc.tm_min % 10); /* Units of minutes */
  207. buf[4] = (1 << 7) | /* Binary group flag BGF2 */
  208. (1 << 6) | /* Binary group flag BGF1 */
  209. ((tc.tm_hour / 10) << 4) | /* Tens of hours */
  210. (tc.tm_hour % 10); /* Units of hours */
  211. break;
  212. case dv_audio_source: /* AAUX source pack */
  213. buf[1] = (0 << 7) | /* locked mode */
  214. (1 << 6) | /* reserved -- always 1 */
  215. (dv_audio_frame_size(c->sys, c->frames) -
  216. c->sys->audio_min_samples[0]);
  217. /* # of samples */
  218. buf[2] = (0 << 7) | /* multi-stereo */
  219. (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
  220. (0 << 4) | /* pair bit: 0 -- one pair of channels */
  221. 0; /* audio mode */
  222. buf[3] = (1 << 7) | /* res */
  223. (1 << 6) | /* multi-language flag */
  224. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  225. (apt << 1);/* definition: 0 -- 25Mbps, 2 -- 50Mbps */
  226. buf[4] = (1 << 7) | /* emphasis: 1 -- off */
  227. (0 << 6) | /* emphasis time constant: 0 -- reserved */
  228. (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */
  229. 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
  230. break;
  231. case dv_audio_control:
  232. buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
  233. (1 << 4) | /* input source: 1 -- digital input */
  234. (3 << 2) | /* compression: 3 -- no information */
  235. 0; /* misc. info/SMPTE emphasis off */
  236. buf[2] = (1 << 7) | /* recording start point: 1 -- no */
  237. (1 << 6) | /* recording end point: 1 -- no */
  238. (1 << 3) | /* recording mode: 1 -- original */
  239. 7;
  240. buf[3] = (1 << 7) | /* direction: 1 -- forward */
  241. 0x20; /* speed */
  242. buf[4] = (1 << 7) | /* reserved -- always 1 */
  243. 0x7f; /* genre category */
  244. break;
  245. case dv_audio_recdate:
  246. case dv_video_recdate: /* VAUX recording date */
  247. ct = c->start_time + (time_t)(c->frames /
  248. ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
  249. brktimegm(ct, &tc);
  250. buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
  251. /* 0xff is very likely to be "unknown" */
  252. buf[2] = (3 << 6) | /* reserved -- always 1 */
  253. ((tc.tm_mday / 10) << 4) | /* Tens of day */
  254. (tc.tm_mday % 10); /* Units of day */
  255. buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
  256. ((tc.tm_mon / 10) << 4) | /* Tens of month */
  257. (tc.tm_mon % 10); /* Units of month */
  258. buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
  259. (tc.tm_year % 10); /* Units of year */
  260. break;
  261. case dv_audio_rectime: /* AAUX recording time */
  262. case dv_video_rectime: /* VAUX recording time */
  263. ct = c->start_time + (time_t)(c->frames /
  264. ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
  265. brktimegm(ct, &tc);
  266. buf[1] = (3 << 6) | /* reserved -- always 1 */
  267. 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
  268. buf[2] = (1 << 7) | /* reserved -- always 1 */
  269. ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
  270. (tc.tm_sec % 10); /* Units of seconds */
  271. buf[3] = (1 << 7) | /* reserved -- always 1 */
  272. ((tc.tm_min / 10) << 4) | /* Tens of minutes */
  273. (tc.tm_min % 10); /* Units of minutes */
  274. buf[4] = (3 << 6) | /* reserved -- always 1 */
  275. ((tc.tm_hour / 10) << 4) | /* Tens of hours */
  276. (tc.tm_hour % 10); /* Units of hours */
  277. break;
  278. case dv_video_source:
  279. buf[1] = 0xff; /* reserved -- always 1 */
  280. buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
  281. (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
  282. (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
  283. 0xf; /* reserved -- always 1 */
  284. buf[3] = (3 << 6) | /* reserved -- always 1 */
  285. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  286. (apt << 2); /* signal type video compression */
  287. buf[4] = 0xff; /* VISC: 0xff -- no information */
  288. break;
  289. case dv_video_control:
  290. buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
  291. 0x3f; /* reserved -- always 1 */
  292. buf[2] = 0xc8 | /* reserved -- always b11001xxx */
  293. c->aspect;
  294. buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */
  295. (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */
  296. (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */
  297. (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
  298. 0xc; /* reserved -- always b1100 */
  299. buf[4] = 0xff; /* reserved -- always 1 */
  300. break;
  301. default:
  302. buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
  303. }
  304. return 5;
  305. }
  306. static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num, uint8_t seq_num,
  307. uint8_t dif_num, uint8_t* buf)
  308. {
  309. buf[0] = (uint8_t)t; /* Section type */
  310. buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
  311. (chan_num << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
  312. 7; /* reserved -- always 1 */
  313. buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
  314. return 3;
  315. }
  316. static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
  317. {
  318. if (syb_num == 0 || syb_num == 6) {
  319. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  320. (0<<4) | /* AP3 (Subcode application ID) */
  321. 0x0f; /* reserved -- always 1 */
  322. }
  323. else if (syb_num == 11) {
  324. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  325. 0x7f; /* reserved -- always 1 */
  326. }
  327. else {
  328. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  329. (0<<4) | /* APT (Track application ID) */
  330. 0x0f; /* reserved -- always 1 */
  331. }
  332. buf[1] = 0xf0 | /* reserved -- always 1 */
  333. (syb_num & 0x0f); /* SSYB number 0 - 11 */
  334. buf[2] = 0xff; /* reserved -- always 1 */
  335. return 3;
  336. }
  337. static void dv_format_frame(DVMuxContext *c, uint8_t* buf)
  338. {
  339. int chan, i, j, k;
  340. for (chan = 0; chan < c->sys->n_difchan; chan++) {
  341. for (i = 0; i < c->sys->difseg_size; i++) {
  342. memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
  343. /* DV header: 1DIF */
  344. buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
  345. buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
  346. buf += 72; /* unused bytes */
  347. /* DV subcode: 2DIFs */
  348. for (j = 0; j < 2; j++) {
  349. buf += dv_write_dif_id(dv_sect_subcode, chan, i, j, buf);
  350. for (k = 0; k < 6; k++) {
  351. buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf);
  352. buf += dv_write_pack(dv_ssyb_packs_dist[i][k], c, buf);
  353. }
  354. buf += 29; /* unused bytes */
  355. }
  356. /* DV VAUX: 3DIFs */
  357. for (j = 0; j < 3; j++) {
  358. buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
  359. for (k = 0; k < 15 ; k++)
  360. buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf);
  361. buf += 2; /* unused bytes */
  362. }
  363. /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
  364. for (j = 0; j < 135; j++) {
  365. if (j%15 == 0) {
  366. memset(buf, 0xff, 80);
  367. buf += dv_write_dif_id(dv_sect_audio, chan, i, j/15, buf);
  368. buf += 77; /* audio control & shuffled PCM audio */
  369. }
  370. buf += dv_write_dif_id(dv_sect_video, chan, i, j, buf);
  371. buf += 77; /* 1 video macro block: 1 bytes control
  372. 4 * 14 bytes Y 8x8 data
  373. 10 bytes Cr 8x8 data
  374. 10 bytes Cb 8x8 data */
  375. }
  376. }
  377. }
  378. }
  379. static void dv_inject_audio(DVMuxContext *c, const uint8_t* pcm, int channel, uint8_t* frame_ptr)
  380. {
  381. int i, j, d, of, size;
  382. size = 4 * dv_audio_frame_size(c->sys, c->frames);
  383. frame_ptr += channel * c->sys->difseg_size * 150 * 80;
  384. for (i = 0; i < c->sys->difseg_size; i++) {
  385. frame_ptr += 6 * 80; /* skip DIF segment header */
  386. for (j = 0; j < 9; j++) {
  387. dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3]);
  388. for (d = 8; d < 80; d+=2) {
  389. of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
  390. if (of*2 >= size)
  391. continue;
  392. frame_ptr[d] = pcm[of*2+1]; // FIXME: may be we have to admit
  393. frame_ptr[d+1] = pcm[of*2]; // that DV is a big endian PCM
  394. }
  395. frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  396. }
  397. }
  398. }
  399. static void dv_inject_video(DVMuxContext *c, const uint8_t* video_data, uint8_t* frame_ptr)
  400. {
  401. int chan, i, j;
  402. int ptr = 0;
  403. for (chan = 0; chan < c->sys->n_difchan; chan++) {
  404. for (i = 0; i < c->sys->difseg_size; i++) {
  405. ptr += 6 * 80; /* skip DIF segment header */
  406. for (j = 0; j < 135; j++) {
  407. if (j%15 == 0)
  408. ptr += 80; /* skip Audio DIF */
  409. ptr += 3;
  410. memcpy(frame_ptr + ptr, video_data + ptr, 77);
  411. ptr += 77;
  412. }
  413. }
  414. }
  415. }
  416. /*
  417. * This is the dumbest implementation of all -- it simply looks at
  418. * a fixed offset and if pack isn't there -- fails. We might want
  419. * to have a fallback mechanism for complete search of missing packs.
  420. */
  421. static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
  422. {
  423. int offs;
  424. switch (t) {
  425. case dv_audio_source:
  426. offs = (80*6 + 80*16*3 + 3);
  427. break;
  428. case dv_audio_control:
  429. offs = (80*6 + 80*16*4 + 3);
  430. break;
  431. case dv_video_control:
  432. offs = (80*5 + 48 + 5);
  433. break;
  434. default:
  435. return NULL;
  436. }
  437. return (frame[offs] == t ? &frame[offs] : NULL);
  438. }
  439. /*
  440. * There's a couple of assumptions being made here:
  441. * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
  442. * We can pass them upwards when ffmpeg will be ready to deal with them.
  443. * 2. We don't do software emphasis.
  444. * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
  445. * are converted into 16bit linear ones.
  446. */
  447. static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2,
  448. const DVprofile *sys)
  449. {
  450. int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
  451. uint16_t lc, rc;
  452. const uint8_t* as_pack;
  453. as_pack = dv_extract_pack(frame, dv_audio_source);
  454. if (!as_pack) /* No audio ? */
  455. return 0;
  456. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  457. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  458. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  459. if (quant > 1)
  460. return -1; /* Unsupported quantization */
  461. size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
  462. half_ch = sys->difseg_size/2;
  463. /* for each DIF channel */
  464. for (chan = 0; chan < sys->n_difchan; chan++) {
  465. /* for each DIF segment */
  466. for (i = 0; i < sys->difseg_size; i++) {
  467. frame += 6 * 80; /* skip DIF segment header */
  468. if (quant == 1 && i == half_ch) {
  469. /* next stereo channel (12bit mode only) */
  470. if (!pcm2)
  471. break;
  472. else
  473. pcm = pcm2;
  474. }
  475. /* for each AV sequence */
  476. for (j = 0; j < 9; j++) {
  477. for (d = 8; d < 80; d += 2) {
  478. if (quant == 0) { /* 16bit quantization */
  479. of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride;
  480. if (of*2 >= size)
  481. continue;
  482. pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit
  483. pcm[of*2+1] = frame[d]; // that DV is a big endian PCM
  484. if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
  485. pcm[of*2+1] = 0;
  486. } else { /* 12bit quantization */
  487. lc = ((uint16_t)frame[d] << 4) |
  488. ((uint16_t)frame[d+2] >> 4);
  489. rc = ((uint16_t)frame[d+1] << 4) |
  490. ((uint16_t)frame[d+2] & 0x0f);
  491. lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
  492. rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
  493. of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride;
  494. if (of*2 >= size)
  495. continue;
  496. pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit
  497. pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM
  498. of = sys->audio_shuffle[i%half_ch+half_ch][j] +
  499. (d - 8)/3 * sys->audio_stride;
  500. pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit
  501. pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM
  502. ++d;
  503. }
  504. }
  505. frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  506. }
  507. }
  508. /* next stereo channel (50Mbps only) */
  509. if(!pcm2)
  510. break;
  511. pcm = pcm2;
  512. }
  513. return size;
  514. }
  515. static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
  516. {
  517. const uint8_t* as_pack;
  518. int freq, stype, smpls, quant, i, ach;
  519. as_pack = dv_extract_pack(frame, dv_audio_source);
  520. if (!as_pack || !c->sys) { /* No audio ? */
  521. c->ach = 0;
  522. return 0;
  523. }
  524. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  525. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  526. stype = (as_pack[3] & 0x1f); /* 0 - 2CH, 2 - 4CH */
  527. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  528. /* note: ach counts PAIRS of channels (i.e. stereo channels) */
  529. ach = (stype == 2 || (quant && (freq == 2))) ? 2 : 1;
  530. /* Dynamic handling of the audio streams in DV */
  531. for (i=0; i<ach; i++) {
  532. if (!c->ast[i]) {
  533. c->ast[i] = av_new_stream(c->fctx, 0);
  534. if (!c->ast[i])
  535. break;
  536. av_set_pts_info(c->ast[i], 64, 1, 30000);
  537. c->ast[i]->codec->codec_type = CODEC_TYPE_AUDIO;
  538. c->ast[i]->codec->codec_id = CODEC_ID_PCM_S16LE;
  539. av_init_packet(&c->audio_pkt[i]);
  540. c->audio_pkt[i].size = 0;
  541. c->audio_pkt[i].data = c->audio_buf[i];
  542. c->audio_pkt[i].stream_index = c->ast[i]->index;
  543. c->audio_pkt[i].flags |= PKT_FLAG_KEY;
  544. }
  545. c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
  546. c->ast[i]->codec->channels = 2;
  547. c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16;
  548. c->ast[i]->start_time = 0;
  549. }
  550. c->ach = i;
  551. return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
  552. }
  553. static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
  554. {
  555. const uint8_t* vsc_pack;
  556. AVCodecContext* avctx;
  557. int apt, is16_9;
  558. int size = 0;
  559. if (c->sys) {
  560. avctx = c->vst->codec;
  561. av_set_pts_info(c->vst, 64, c->sys->frame_rate_base, c->sys->frame_rate);
  562. avctx->time_base= (AVRational){c->sys->frame_rate_base, c->sys->frame_rate};
  563. if(!avctx->width){
  564. avctx->width = c->sys->width;
  565. avctx->height = c->sys->height;
  566. }
  567. avctx->pix_fmt = c->sys->pix_fmt;
  568. /* finding out SAR is a little bit messy */
  569. vsc_pack = dv_extract_pack(frame, dv_video_control);
  570. apt = frame[4] & 0x07;
  571. is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
  572. (!apt && (vsc_pack[2] & 0x07) == 0x07)));
  573. avctx->sample_aspect_ratio = c->sys->sar[is16_9];
  574. avctx->bit_rate = av_rescale(c->sys->frame_size * 8,
  575. c->sys->frame_rate,
  576. c->sys->frame_rate_base);
  577. size = c->sys->frame_size;
  578. }
  579. return size;
  580. }
  581. /*
  582. * The following 6 functions constitute our interface to the world
  583. */
  584. int dv_assemble_frame(DVMuxContext *c, AVStream* st,
  585. const uint8_t* data, int data_size, uint8_t** frame)
  586. {
  587. uint8_t pcm[8192];
  588. int i;
  589. *frame = &c->frame_buf[0];
  590. if (c->has_audio && c->has_video &&
  591. (c->has_audio == -1 || c->has_audio == c->n_ast)) {
  592. /* must be a stale frame */
  593. dv_format_frame(c, *frame);
  594. c->frames++;
  595. if (c->has_audio > 0)
  596. c->has_audio = 0;
  597. c->has_video = 0;
  598. }
  599. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  600. /* FIXME: we have to have more sensible approach than this one */
  601. if (c->has_video)
  602. av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
  603. dv_inject_video(c, data, *frame);
  604. c->has_video = 1;
  605. data_size = 0;
  606. if (c->has_audio < 0)
  607. goto out;
  608. }
  609. for (i = 0; i < c->n_ast; i++) {
  610. int reqasize, fsize;
  611. if (st != c->ast[i])
  612. continue;
  613. reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
  614. fsize = fifo_size(&c->audio_data[i], c->audio_data[i].rptr);
  615. if (st->codec->codec_type == CODEC_TYPE_AUDIO ||
  616. (c->has_video && fsize >= reqasize)) {
  617. if (fsize + data_size >= reqasize && (c->has_audio < c->n_ast)) {
  618. if (fsize >= reqasize) {
  619. fifo_read(&c->audio_data[i], &pcm[0], reqasize, &c->audio_data[i].rptr);
  620. } else {
  621. fifo_read(&c->audio_data[i], &pcm[0], fsize, &c->audio_data[i].rptr);
  622. memcpy(&pcm[fsize], &data[0], reqasize - fsize);
  623. data += reqasize - fsize;
  624. data_size -= reqasize - fsize;
  625. }
  626. dv_inject_audio(c, &pcm[0], i, *frame);
  627. c->has_audio += 1;
  628. }
  629. /* FIXME: we have to have more sensible approach than this one */
  630. if (fifo_size(&c->audio_data[i], c->audio_data[i].rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
  631. av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
  632. fifo_write(&c->audio_data[i], (uint8_t *)data, data_size, &c->audio_data[i].wptr);
  633. }
  634. }
  635. out:
  636. return ((c->has_audio == -1 || c->has_audio == c->n_ast) && c->has_video) ? c->sys->frame_size : 0;
  637. }
  638. DVMuxContext* dv_init_mux(AVFormatContext* s)
  639. {
  640. DVMuxContext *c;
  641. AVStream *vst = NULL;
  642. int i;
  643. /* we support at most 1 video and 2 audio streams */
  644. if (s->nb_streams > 3)
  645. return NULL;
  646. c = av_mallocz(sizeof(DVMuxContext));
  647. if (!c)
  648. return NULL;
  649. c->n_ast = 0;
  650. c->ast[0] = c->ast[1] = NULL;
  651. /* We have to sort out where audio and where video stream is */
  652. for (i=0; i<s->nb_streams; i++) {
  653. switch (s->streams[i]->codec->codec_type) {
  654. case CODEC_TYPE_VIDEO:
  655. vst = s->streams[i];
  656. break;
  657. case CODEC_TYPE_AUDIO:
  658. c->ast[c->n_ast++] = s->streams[i];
  659. break;
  660. default:
  661. goto bail_out;
  662. }
  663. }
  664. /* Some checks -- DV format is very picky about its incoming streams */
  665. if (!vst || vst->codec->codec_id != CODEC_ID_DVVIDEO)
  666. goto bail_out;
  667. for (i=0; i<c->n_ast; i++) {
  668. if (c->ast[i] && (c->ast[i]->codec->codec_id != CODEC_ID_PCM_S16LE ||
  669. c->ast[i]->codec->sample_rate != 48000 ||
  670. c->ast[i]->codec->channels != 2))
  671. goto bail_out;
  672. }
  673. c->sys = dv_codec_profile(vst->codec);
  674. if (!c->sys)
  675. goto bail_out;
  676. if((c->n_ast > 1) && (c->sys->n_difchan < 2)) {
  677. /* only 1 stereo pair is allowed in 25Mbps mode */
  678. goto bail_out;
  679. }
  680. /* Ok, everything seems to be in working order */
  681. c->frames = 0;
  682. c->has_audio = c->n_ast ? 0 : -1;
  683. c->has_video = 0;
  684. c->start_time = (time_t)s->timestamp;
  685. c->aspect = 0; /* 4:3 is the default */
  686. if ((int)(av_q2d(vst->codec->sample_aspect_ratio) * vst->codec->width / vst->codec->height * 10) == 17) /* 16:9 */
  687. c->aspect = 0x07;
  688. for (i=0; i<c->n_ast; i++) {
  689. if (c->ast[i] && fifo_init(&c->audio_data[i], 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) {
  690. while (i>0) {
  691. i--;
  692. fifo_free(&c->audio_data[i]);
  693. }
  694. goto bail_out;
  695. }
  696. }
  697. dv_format_frame(c, &c->frame_buf[0]);
  698. return c;
  699. bail_out:
  700. av_free(c);
  701. return NULL;
  702. }
  703. void dv_delete_mux(DVMuxContext *c)
  704. {
  705. int i;
  706. for (i=0; i < c->n_ast; i++)
  707. fifo_free(&c->audio_data[i]);
  708. }
  709. DVDemuxContext* dv_init_demux(AVFormatContext *s)
  710. {
  711. DVDemuxContext *c;
  712. c = av_mallocz(sizeof(DVDemuxContext));
  713. if (!c)
  714. return NULL;
  715. c->vst = av_new_stream(s, 0);
  716. if (!c->vst) {
  717. av_free(c);
  718. return NULL;
  719. }
  720. c->sys = NULL;
  721. c->fctx = s;
  722. c->ast[0] = c->ast[1] = NULL;
  723. c->ach = 0;
  724. c->frames = 0;
  725. c->abytes = 0;
  726. c->vst->codec->codec_type = CODEC_TYPE_VIDEO;
  727. c->vst->codec->codec_id = CODEC_ID_DVVIDEO;
  728. c->vst->codec->bit_rate = 25000000;
  729. c->vst->start_time = 0;
  730. return c;
  731. }
  732. int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
  733. {
  734. int size = -1;
  735. int i;
  736. for (i=0; i<c->ach; i++) {
  737. if (c->ast[i] && c->audio_pkt[i].size) {
  738. *pkt = c->audio_pkt[i];
  739. c->audio_pkt[i].size = 0;
  740. size = pkt->size;
  741. break;
  742. }
  743. }
  744. return size;
  745. }
  746. int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
  747. uint8_t* buf, int buf_size)
  748. {
  749. int size, i;
  750. if (buf_size < 4 || buf_size < c->sys->frame_size)
  751. return -1; /* Broken frame, or not enough data */
  752. /* Queueing audio packet */
  753. /* FIXME: in case of no audio/bad audio we have to do something */
  754. size = dv_extract_audio_info(c, buf);
  755. for (i=0; i<c->ach; i++) {
  756. c->audio_pkt[i].size = size;
  757. c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
  758. }
  759. dv_extract_audio(buf, c->audio_buf[0], c->audio_buf[1], c->sys);
  760. c->abytes += size;
  761. /* Now it's time to return video packet */
  762. size = dv_extract_video_info(c, buf);
  763. av_init_packet(pkt);
  764. pkt->data = buf;
  765. pkt->size = size;
  766. pkt->flags |= PKT_FLAG_KEY;
  767. pkt->stream_index = c->vst->id;
  768. pkt->pts = c->frames;
  769. c->frames++;
  770. return size;
  771. }
  772. static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
  773. int64_t timestamp, int flags)
  774. {
  775. // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
  776. const DVprofile* sys = dv_codec_profile(c->vst->codec);
  777. int64_t offset;
  778. int64_t size = url_fsize(&s->pb);
  779. int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size;
  780. offset = sys->frame_size * timestamp;
  781. if (offset > max_offset) offset = max_offset;
  782. else if (offset < 0) offset = 0;
  783. return offset;
  784. }
  785. void dv_flush_audio_packets(DVDemuxContext *c)
  786. {
  787. c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
  788. }
  789. /************************************************************
  790. * Implementation of the easiest DV storage of all -- raw DV.
  791. ************************************************************/
  792. typedef struct RawDVContext {
  793. DVDemuxContext* dv_demux;
  794. uint8_t buf[DV_MAX_FRAME_SIZE];
  795. } RawDVContext;
  796. static int dv_read_header(AVFormatContext *s,
  797. AVFormatParameters *ap)
  798. {
  799. RawDVContext *c = s->priv_data;
  800. c->dv_demux = dv_init_demux(s);
  801. if (!c->dv_demux)
  802. return -1;
  803. if (get_buffer(&s->pb, c->buf, DV_PROFILE_BYTES) <= 0 ||
  804. url_fseek(&s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
  805. return AVERROR_IO;
  806. c->dv_demux->sys = dv_frame_profile(c->buf);
  807. s->bit_rate = av_rescale(c->dv_demux->sys->frame_size * 8,
  808. c->dv_demux->sys->frame_rate,
  809. c->dv_demux->sys->frame_rate_base);
  810. return 0;
  811. }
  812. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  813. {
  814. int size;
  815. RawDVContext *c = s->priv_data;
  816. size = dv_get_packet(c->dv_demux, pkt);
  817. if (size < 0) {
  818. size = c->dv_demux->sys->frame_size;
  819. if (get_buffer(&s->pb, c->buf, size) <= 0)
  820. return AVERROR_IO;
  821. size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
  822. }
  823. return size;
  824. }
  825. static int dv_read_seek(AVFormatContext *s, int stream_index,
  826. int64_t timestamp, int flags)
  827. {
  828. RawDVContext *r = s->priv_data;
  829. DVDemuxContext *c = r->dv_demux;
  830. int64_t offset= dv_frame_offset(s, c, timestamp, flags);
  831. c->frames= offset / c->sys->frame_size;
  832. if (c->ach)
  833. c->abytes= av_rescale(c->frames,
  834. c->ast[0]->codec->bit_rate * (int64_t)c->sys->frame_rate_base,
  835. 8*c->sys->frame_rate);
  836. dv_flush_audio_packets(c);
  837. return url_fseek(&s->pb, offset, SEEK_SET);
  838. }
  839. static int dv_read_close(AVFormatContext *s)
  840. {
  841. RawDVContext *c = s->priv_data;
  842. av_free(c->dv_demux);
  843. return 0;
  844. }
  845. #ifdef CONFIG_MUXERS
  846. static int dv_write_header(AVFormatContext *s)
  847. {
  848. s->priv_data = dv_init_mux(s);
  849. if (!s->priv_data) {
  850. av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
  851. "Make sure that you supply exactly two streams:\n"
  852. " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n"
  853. " (50Mbps allows an optional second audio stream)\n");
  854. return -1;
  855. }
  856. return 0;
  857. }
  858. static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  859. {
  860. uint8_t* frame;
  861. int fsize;
  862. fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index],
  863. pkt->data, pkt->size, &frame);
  864. if (fsize > 0) {
  865. put_buffer(&s->pb, frame, fsize);
  866. put_flush_packet(&s->pb);
  867. }
  868. return 0;
  869. }
  870. /*
  871. * We might end up with some extra A/V data without matching counterpart.
  872. * E.g. video data without enough audio to write the complete frame.
  873. * Currently we simply drop the last frame. I don't know whether this
  874. * is the best strategy of all
  875. */
  876. static int dv_write_trailer(struct AVFormatContext *s)
  877. {
  878. dv_delete_mux((DVMuxContext *)s->priv_data);
  879. return 0;
  880. }
  881. #endif /* CONFIG_MUXERS */
  882. static AVInputFormat dv_iformat = {
  883. "dv",
  884. "DV video format",
  885. sizeof(RawDVContext),
  886. NULL,
  887. dv_read_header,
  888. dv_read_packet,
  889. dv_read_close,
  890. dv_read_seek,
  891. .extensions = "dv,dif",
  892. };
  893. #ifdef CONFIG_MUXERS
  894. static AVOutputFormat dv_oformat = {
  895. "dv",
  896. "DV video format",
  897. NULL,
  898. "dv",
  899. sizeof(DVMuxContext),
  900. CODEC_ID_PCM_S16LE,
  901. CODEC_ID_DVVIDEO,
  902. dv_write_header,
  903. dv_write_packet,
  904. dv_write_trailer,
  905. };
  906. #endif
  907. int ff_dv_init(void)
  908. {
  909. av_register_input_format(&dv_iformat);
  910. #ifdef CONFIG_MUXERS
  911. av_register_output_format(&dv_oformat);
  912. #endif
  913. return 0;
  914. }