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.

669 lines
25KB

  1. /*
  2. * DV format 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. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "avformat.h"
  23. #include "dvcore.h"
  24. /*
  25. * The reason why the following three big ugly looking tables are
  26. * here is my lack of DV spec IEC 61834. The tables were basically
  27. * constructed to make code that places packs in SSYB, VAUX and
  28. * AAUX blocks very simple and table-driven. They conform to the
  29. * SMPTE 314M and the output of my personal DV camcorder, neither
  30. * of which is sufficient for a reliable DV stream producing. Thus
  31. * while code is still in development I'll be gathering input from
  32. * people with different DV equipment and modifying the tables to
  33. * accommodate all the quirks. Later on, if possible, some of them
  34. * will be folded into smaller tables and/or switch-if logic. For
  35. * now, my only excuse is -- they don't eat up that much of a space.
  36. */
  37. static const int dv_ssyb_packs_dist[12][6] = {
  38. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  39. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  40. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  41. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  42. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  43. { 0x13, 0x13, 0x13, 0x13, 0x13, 0x13 },
  44. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  45. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  46. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  47. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  48. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  49. { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
  50. };
  51. static const int dv_vaux_packs_dist[12][15] = {
  52. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  53. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  54. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  55. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  56. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  57. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  58. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  59. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  60. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  61. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  62. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  63. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  64. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  65. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  66. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  67. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  68. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  69. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  70. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  71. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  72. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  73. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  74. { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
  75. 0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
  76. };
  77. static const int dv_aaux_packs_dist[12][9] = {
  78. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  79. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  80. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  81. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  82. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  83. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  84. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  85. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  86. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  87. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  88. { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
  89. { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
  90. };
  91. enum dv_section_type {
  92. dv_sect_header = 0x1f,
  93. dv_sect_subcode = 0x3f,
  94. dv_sect_vaux = 0x56,
  95. dv_sect_audio = 0x76,
  96. dv_sect_video = 0x96,
  97. };
  98. enum dv_pack_type {
  99. dv_header525 = 0x3f, /* see dv_write_pack for important details on */
  100. dv_header625 = 0xbf, /* these two packs */
  101. dv_timecode = 0x13,
  102. dv_audio_source = 0x50,
  103. dv_audio_control = 0x51,
  104. dv_audio_recdate = 0x52,
  105. dv_audio_rectime = 0x53,
  106. dv_video_source = 0x60,
  107. dv_video_control = 0x61,
  108. dv_viedo_recdate = 0x62,
  109. dv_video_rectime = 0x63,
  110. dv_unknown_pack = 0xff,
  111. };
  112. static const uint16_t dv_audio_shuffle525[10][9] = {
  113. { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
  114. { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
  115. { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
  116. { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
  117. { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
  118. { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
  119. { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
  120. { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
  121. { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
  122. { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
  123. };
  124. static const uint16_t dv_audio_shuffle625[12][9] = {
  125. { 0, 36, 72, 26, 62, 98, 16, 52, 88}, /* 1st channel */
  126. { 6, 42, 78, 32, 68, 104, 22, 58, 94},
  127. { 12, 48, 84, 2, 38, 74, 28, 64, 100},
  128. { 18, 54, 90, 8, 44, 80, 34, 70, 106},
  129. { 24, 60, 96, 14, 50, 86, 4, 40, 76},
  130. { 30, 66, 102, 20, 56, 92, 10, 46, 82},
  131. { 1, 37, 73, 27, 63, 99, 17, 53, 89}, /* 2nd channel */
  132. { 7, 43, 79, 33, 69, 105, 23, 59, 95},
  133. { 13, 49, 85, 3, 39, 75, 29, 65, 101},
  134. { 19, 55, 91, 9, 45, 81, 35, 71, 107},
  135. { 25, 61, 97, 15, 51, 87, 5, 41, 77},
  136. { 31, 67, 103, 21, 57, 93, 11, 47, 83},
  137. };
  138. static const int dv_audio_frequency[3] = {
  139. 48000, 44100, 32000,
  140. };
  141. const DVprofile dv_profiles[2] = {
  142. { .dsf = 0,
  143. .frame_size = 120000, /* 525/60 system (NTSC) */
  144. .difseg_size = 10,
  145. .frame_rate = 30000,
  146. .ltc_divisor = 30,
  147. .frame_rate_base = 1001,
  148. .height = 480,
  149. /* .video_place = dv_place_411, */
  150. .audio_stride = 90,
  151. .audio_min_samples = { 1580, 1452, 1053 }, /* for 48, 44.1 and 32Khz */
  152. .audio_samples_dist = { 1602, 1601, 1602, 1601, 1602 },
  153. .audio_shuffle = dv_audio_shuffle525,
  154. },
  155. { .dsf = 1,
  156. .frame_size = 144000, /* 625/50 system (PAL) */
  157. .difseg_size = 12,
  158. .frame_rate = 25,
  159. .frame_rate_base = 1,
  160. .ltc_divisor = 25,
  161. .height = 576,
  162. /* .video_place = dv_place_420, */
  163. .audio_stride = 108,
  164. .audio_min_samples = { 1896, 1742, 1264 }, /* for 48, 44.1 and 32Khz */
  165. .audio_samples_dist = { 1920, 1920, 1920, 1920, 1920 },
  166. .audio_shuffle = dv_audio_shuffle525,
  167. }
  168. };
  169. static inline uint16_t dv_audio_12to16(uint16_t sample)
  170. {
  171. uint16_t shift, result;
  172. sample = (sample < 0x800) ? sample : sample | 0xf000;
  173. shift = (sample & 0xf00) >> 8;
  174. if (shift < 0x2 || shift > 0xd) {
  175. result = sample;
  176. } else if (shift < 0x8) {
  177. shift--;
  178. result = (sample - (256 * shift)) << shift;
  179. } else {
  180. shift = 0xe - shift;
  181. result = ((sample + ((256 * shift) + 1)) << shift) - 1;
  182. }
  183. return result;
  184. }
  185. static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf)
  186. {
  187. struct tm tc;
  188. time_t ct;
  189. int ltc_frame;
  190. buf[0] = (uint8_t)pack_id;
  191. switch (pack_id) {
  192. case dv_header525: /* I can't imagine why these two weren't defined as real */
  193. case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
  194. buf[1] = 0xf8 | /* reserved -- always 1 */
  195. (0 & 0x07); /* APT: Track application ID */
  196. buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
  197. (0x0f << 3) | /* reserved -- always 1 */
  198. (0 & 0x07); /* AP1: Audio application ID */
  199. buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
  200. (0x0f << 3) | /* reserved -- always 1 */
  201. (0 & 0x07); /* AP2: Video application ID */
  202. buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
  203. (0x0f << 3) | /* reserved -- always 1 */
  204. (0 & 0x07); /* AP3: Subcode application ID */
  205. break;
  206. case dv_timecode:
  207. ct = (time_t)(c->frames / ((float)c->sys->frame_rate /
  208. (float)c->sys->frame_rate_base));
  209. localtime_r(&ct, &tc);
  210. /*
  211. * LTC drop-frame frame counter drops two frames (0 and 1) every
  212. * minute, unless it is exactly divisible by 10
  213. */
  214. ltc_frame = (c->frames + 2*ct/60 - 2*ct/600) % c->sys->ltc_divisor;
  215. buf[1] = (0 << 7) | /* Color fame: 0 - unsync; 1 - sync mode */
  216. (1 << 6) | /* Drop frame timecode: 0 - nondrop; 1 - drop */
  217. ((ltc_frame / 10) << 4) | /* Tens of frames */
  218. (ltc_frame % 10); /* Units of frames */
  219. buf[2] = (1 << 7) | /* Biphase mark polarity correction: 0 - even; 1 - odd */
  220. ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
  221. (tc.tm_sec % 10); /* Units of seconds */
  222. buf[3] = (1 << 7) | /* Binary group flag BGF0 */
  223. ((tc.tm_min / 10) << 4) | /* Tens of minutes */
  224. (tc.tm_min % 10); /* Units of minutes */
  225. buf[4] = (1 << 7) | /* Binary group flag BGF2 */
  226. (1 << 6) | /* Binary group flag BGF1 */
  227. ((tc.tm_hour / 10) << 4) | /* Tens of hours */
  228. (tc.tm_hour % 10); /* Units of hours */
  229. break;
  230. case dv_audio_source: /* AAUX source pack */
  231. buf[1] = (0 << 7) | /* locked mode */
  232. (1 << 6) | /* reserved -- always 1 */
  233. (dv_audio_frame_size(c->sys, c->frames) -
  234. c->sys->audio_min_samples[0]);
  235. /* # of samples */
  236. buf[2] = (0 << 7) | /* multi-stereo */
  237. (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
  238. (0 << 4) | /* pair bit: 0 -- one pair of channels */
  239. 0; /* audio mode */
  240. buf[3] = (1 << 7) | /* res */
  241. (1 << 6) | /* multi-language flag */
  242. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  243. 0; /* definition: 0 -- SD (525/625) */
  244. buf[4] = (1 << 7) | /* emphasis: 1 -- off */
  245. (0 << 6) | /* emphasis time constant: 0 -- reserved */
  246. (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */
  247. 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
  248. break;
  249. case dv_audio_control:
  250. buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
  251. (1 << 4) | /* input source: 1 -- digital input */
  252. (3 << 2) | /* compression: 3 -- no information */
  253. 0; /* misc. info/SMPTE emphasis off */
  254. buf[2] = (1 << 7) | /* recording start point: 1 -- no */
  255. (1 << 6) | /* recording end point: 1 -- no */
  256. (1 << 3) | /* recording mode: 1 -- original */
  257. 7;
  258. buf[3] = (1 << 7) | /* direction: 1 -- forward */
  259. 0x20; /* speed */
  260. buf[4] = (1 << 7) | /* reserved -- always 1 */
  261. 0x7f; /* genre category */
  262. break;
  263. case dv_audio_recdate:
  264. case dv_viedo_recdate: /* VAUX recording date */
  265. ct = c->start_time + (time_t)(c->frames /
  266. ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
  267. localtime_r(&ct, &tc);
  268. buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
  269. /* 0xff is very likely to be "unknown" */
  270. buf[2] = (3 << 6) | /* reserved -- always 1 */
  271. ((tc.tm_mday / 10) << 4) | /* Tens of day */
  272. (tc.tm_mday % 10); /* Units of day */
  273. buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
  274. (tc.tm_mon % 10); /* Units of month */
  275. buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
  276. (tc.tm_year % 10); /* Units of year */
  277. break;
  278. case dv_audio_rectime: /* AAUX recording time */
  279. case dv_video_rectime: /* VAUX recording time */
  280. ct = c->start_time + (time_t)(c->frames /
  281. ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
  282. localtime_r(&ct, &tc);
  283. buf[1] = (3 << 6) | /* reserved -- always 1 */
  284. 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
  285. buf[2] = (1 << 7) | /* reserved -- always 1 */
  286. ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
  287. (tc.tm_sec % 10); /* Units of seconds */
  288. buf[3] = (1 << 7) | /* reserved -- always 1 */
  289. ((tc.tm_min / 10) << 4) | /* Tens of minutes */
  290. (tc.tm_min % 10); /* Units of minutes */
  291. buf[4] = (3 << 6) | /* reserved -- always 1 */
  292. ((tc.tm_hour / 10) << 4) | /* Tens of hours */
  293. (tc.tm_hour % 10); /* Units of hours */
  294. break;
  295. case dv_video_source:
  296. buf[1] = 0xff; /* reserved -- always 1 */
  297. buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
  298. (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
  299. (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
  300. 0xf; /* reserved -- always 1 */
  301. buf[3] = (3 << 6) | /* reserved -- always 1 */
  302. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  303. 0; /* signal type video compression */
  304. buf[4] = 0xff; /* VISC: 0xff -- no information */
  305. break;
  306. case dv_video_control:
  307. buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
  308. 0x3f; /* reserved -- always 1 */
  309. buf[2] = 0xc8 | /* reserved -- always b11001xxx */
  310. c->aspect;
  311. buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */
  312. (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */
  313. (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */
  314. (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
  315. 0xc; /* reserved -- always b1100 */
  316. buf[4] = 0xff; /* reserved -- always 1 */
  317. break;
  318. default:
  319. buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
  320. }
  321. return 5;
  322. }
  323. static inline int dv_write_dif_id(enum dv_section_type t, uint8_t seq_num,
  324. uint8_t dif_num, uint8_t* buf)
  325. {
  326. buf[0] = (uint8_t)t; /* Section type */
  327. buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
  328. (0 << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
  329. 7; /* reserved -- always 1 */
  330. buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
  331. return 3;
  332. }
  333. static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
  334. {
  335. if (syb_num == 0 || syb_num == 6) {
  336. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  337. (0<<4) | /* AP3 (Subcode application ID) */
  338. 0x0f; /* reserved -- always 1 */
  339. }
  340. else if (syb_num == 11) {
  341. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  342. 0x7f; /* reserved -- always 1 */
  343. }
  344. else {
  345. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  346. (0<<4) | /* APT (Track application ID) */
  347. 0x0f; /* reserved -- always 1 */
  348. }
  349. buf[1] = 0xf0 | /* reserved -- always 1 */
  350. (syb_num & 0x0f); /* SSYB number 0 - 11 */
  351. buf[2] = 0xff; /* reserved -- always 1 */
  352. return 3;
  353. }
  354. void dv_format_frame(DVMuxContext *c, uint8_t* buf)
  355. {
  356. int i, j, k;
  357. for (i = 0; i < c->sys->difseg_size; i++) {
  358. memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
  359. /* DV header: 1DIF */
  360. buf += dv_write_dif_id(dv_sect_header, i, 0, buf);
  361. buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
  362. buf += 72; /* unused bytes */
  363. /* DV subcode: 2DIFs */
  364. for (j = 0; j < 2; j++) {
  365. buf += dv_write_dif_id( dv_sect_subcode, i, j, buf);
  366. for (k = 0; k < 6; k++) {
  367. buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf);
  368. buf += dv_write_pack(dv_ssyb_packs_dist[i][k], c, buf);
  369. }
  370. buf += 29; /* unused bytes */
  371. }
  372. /* DV VAUX: 3DIFs */
  373. for (j = 0; j < 3; j++) {
  374. buf += dv_write_dif_id(dv_sect_vaux, i, j, buf);
  375. for (k = 0; k < 15 ; k++)
  376. buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf);
  377. buf += 2; /* unused bytes */
  378. }
  379. /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
  380. for (j = 0; j < 135; j++) {
  381. if (j%15 == 0) {
  382. buf += dv_write_dif_id(dv_sect_audio, i, j/15, buf);
  383. buf += dv_write_pack(dv_aaux_packs_dist[i][j/15], c, buf);
  384. buf += 72; /* shuffled PCM audio */
  385. }
  386. buf += dv_write_dif_id(dv_sect_video, i, j, buf);
  387. buf += 77; /* 1 video macro block: 1 bytes control
  388. 4 * 14 bytes Y 8x8 data
  389. 10 bytes Cr 8x8 data
  390. 10 bytes Cb 8x8 data */
  391. }
  392. }
  393. }
  394. void dv_inject_audio(DVMuxContext *c, const uint8_t* pcm, uint8_t* frame_ptr)
  395. {
  396. int i, j, d, of;
  397. for (i = 0; i < c->sys->difseg_size; i++) {
  398. frame_ptr += 6 * 80; /* skip DIF segment header */
  399. for (j = 0; j < 9; j++) {
  400. for (d = 8; d < 80; d+=2) {
  401. of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
  402. frame_ptr[d] = pcm[of*2+1]; // FIXME: may be we have to admit
  403. frame_ptr[d+1] = pcm[of*2]; // that DV is a big endian PCM
  404. }
  405. frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  406. }
  407. }
  408. }
  409. void dv_inject_video(DVMuxContext *c, const uint8_t* video_data, uint8_t* frame_ptr)
  410. {
  411. int i, j;
  412. int ptr = 0;
  413. for (i = 0; i < c->sys->difseg_size; i++) {
  414. ptr += 6 * 80; /* skip DIF segment header */
  415. for (j = 0; j < 135; j++) {
  416. if (j%15 == 0)
  417. ptr += 80; /* skip Audio DIF */
  418. ptr += 3;
  419. memcpy(frame_ptr + ptr, video_data + ptr, 77);
  420. ptr += 77;
  421. }
  422. }
  423. }
  424. int dv_audio_frame_size(const DVprofile* sys, int frame)
  425. {
  426. return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist)/
  427. sizeof(sys->audio_samples_dist[0]))];
  428. }
  429. const DVprofile* dv_frame_profile(uint8_t* frame)
  430. {
  431. return &dv_profiles[!!(frame[3] & 0x80)]; /* Header, DSF flag */
  432. }
  433. /*
  434. * This is the dumbest implementation of all -- it simply looks at
  435. * a fixed offset and if pack isn't there -- fails. We might want
  436. * to have a fallback mechanism for complete search of missing packs.
  437. */
  438. const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
  439. {
  440. int offs;
  441. switch (t) {
  442. case dv_audio_source:
  443. offs = (80*6 + 80*16*3 + 3);
  444. break;
  445. case dv_audio_control:
  446. offs = (80*6 + 80*16*4 + 3);
  447. break;
  448. case dv_video_control:
  449. offs = (80*5 + 48 + 5);
  450. break;
  451. default:
  452. return NULL;
  453. }
  454. return (frame[offs] == t ? &frame[offs] : NULL);
  455. }
  456. /*
  457. * There's a couple of assumptions being made here:
  458. * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
  459. * We can pass them upwards when ffmpeg will be ready to deal with them.
  460. * 2. We don't do software emphasis.
  461. * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
  462. * are converted into 16bit linear ones.
  463. */
  464. int dv_extract_audio(uint8_t* frame, uint8_t* pcm, AVCodecContext* avctx)
  465. {
  466. int size, i, j, d, of, smpls, freq, quant;
  467. uint16_t lc, rc;
  468. const DVprofile* sys;
  469. const uint8_t* as_pack;
  470. as_pack = dv_extract_pack(frame, dv_audio_source);
  471. if (!as_pack) /* No audio ? */
  472. return 0;
  473. sys = dv_frame_profile(frame);
  474. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  475. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  476. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  477. if (quant > 1)
  478. return -1; /* Unsupported quantization */
  479. avctx->sample_rate = dv_audio_frequency[freq];
  480. avctx->channels = 2;
  481. avctx->bit_rate = avctx->channels * avctx->sample_rate * 16;
  482. // What about:
  483. // avctx->frame_size =
  484. size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
  485. /* for each DIF segment */
  486. for (i = 0; i < sys->difseg_size; i++) {
  487. frame += 6 * 80; /* skip DIF segment header */
  488. for (j = 0; j < 9; j++) {
  489. for (d = 8; d < 80; d += 2) {
  490. if (quant == 0) { /* 16bit quantization */
  491. of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride;
  492. pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit
  493. pcm[of*2+1] = frame[d]; // that DV is a big endian PCM
  494. if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
  495. pcm[of*2+1] = 0;
  496. } else { /* 12bit quantization */
  497. if (i >= sys->difseg_size/2)
  498. goto out; /* We're not doing 4ch at this time */
  499. lc = ((uint16_t)frame[d] << 4) |
  500. ((uint16_t)frame[d+2] >> 4);
  501. rc = ((uint16_t)frame[d+1] << 4) |
  502. ((uint16_t)frame[d+2] & 0x0f);
  503. lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
  504. rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
  505. of = sys->audio_shuffle[i][j] + (d - 8)/3 * sys->audio_stride;
  506. pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit
  507. pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM
  508. of = sys->audio_shuffle[i+sys->difseg_size/2][j] +
  509. (d - 8)/3 * sys->audio_stride;
  510. pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit
  511. pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM
  512. ++d;
  513. }
  514. }
  515. frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  516. }
  517. }
  518. out:
  519. return size;
  520. }
  521. /* FIXME: The following three functions could be underengineered ;-) */
  522. void dv_assemble_frame(DVMuxContext *c, const uint8_t* video, const uint8_t* audio, int asize)
  523. {
  524. uint8_t pcm[8192];
  525. uint8_t* frame = &c->frame_buf[0];
  526. int fsize, reqasize;
  527. if (c->has_audio && c->has_video) { /* must be a stale frame */
  528. dv_format_frame(c, frame);
  529. c->frames++;
  530. c->has_audio = c->has_video = 0;
  531. }
  532. if (video) {
  533. /* FIXME: we have to have more sensible approach than this one */
  534. if (c->has_video)
  535. fprintf(stderr, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
  536. dv_inject_video(c, video, frame);
  537. c->has_video = 1;
  538. }
  539. if (audio) {
  540. reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
  541. fsize = fifo_size(&c->audio_data, c->audio_data.rptr);
  542. if (fsize + asize >= reqasize) {
  543. if (fsize >= reqasize) {
  544. fifo_read(&c->audio_data, &pcm[0], reqasize, &c->audio_data.rptr);
  545. } else {
  546. fifo_read(&c->audio_data, &pcm[0], fsize, &c->audio_data.rptr);
  547. memcpy(&pcm[fsize], &audio[0], reqasize - fsize);
  548. audio += reqasize - fsize;
  549. asize -= reqasize - fsize;
  550. }
  551. dv_inject_audio(c, &pcm[0], frame);
  552. c->has_audio = 1;
  553. }
  554. /* FIXME: we have to have more sensible approach than this one */
  555. if (fifo_size(&c->audio_data, c->audio_data.rptr) + asize >= AVCODEC_MAX_AUDIO_FRAME_SIZE)
  556. fprintf(stderr, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
  557. fifo_write(&c->audio_data, (uint8_t *)audio, asize, &c->audio_data.wptr);
  558. }
  559. }
  560. int dv_core_init(DVMuxContext *c, AVStream *streams[])
  561. {
  562. /* We have to sort out where audio and where video stream is */
  563. if (streams[0]->codec.codec_type == CODEC_TYPE_VIDEO &&
  564. streams[1]->codec.codec_type == CODEC_TYPE_AUDIO) {
  565. c->vst = 0;
  566. c->ast = 1;
  567. }
  568. else if (streams[1]->codec.codec_type == CODEC_TYPE_VIDEO &&
  569. streams[0]->codec.codec_type == CODEC_TYPE_AUDIO) {
  570. c->vst = 1;
  571. c->ast = 0;
  572. } else
  573. goto bail_out;
  574. /* Some checks -- DV format is very picky about its incoming streams */
  575. if (streams[c->vst]->codec.codec_id != CODEC_ID_DVVIDEO ||
  576. streams[c->ast]->codec.codec_id != CODEC_ID_PCM_S16LE)
  577. goto bail_out;
  578. if (streams[c->ast]->codec.sample_rate != 48000 ||
  579. streams[c->ast]->codec.channels != 2)
  580. goto bail_out;
  581. if (streams[c->vst]->codec.frame_rate == 25 &&
  582. streams[c->vst]->codec.frame_rate_base == 1) {
  583. /* May be we have to pick sys for every frame */
  584. c->sys = &dv_profiles[1];
  585. }
  586. else if (streams[c->vst]->codec.frame_rate == 30000 &&
  587. streams[c->vst]->codec.frame_rate_base == 1001) {
  588. /* May be we have to pick sys for every frame */
  589. c->sys = &dv_profiles[0];
  590. } else
  591. goto bail_out;
  592. /* Ok, everything seems to be in working order */
  593. c->frames = 0;
  594. c->has_audio = c->has_video = 0;
  595. c->start_time = time(NULL);
  596. c->aspect = 0; /* 4:3 is the default */
  597. if (streams[c->vst]->codec.aspect_ratio == 16.0 / 9.0)
  598. c->aspect = 0x07;
  599. if (fifo_init(&c->audio_data, AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0)
  600. goto bail_out;
  601. dv_format_frame(c, &c->frame_buf[0]);
  602. return 0;
  603. bail_out:
  604. return -1;
  605. }
  606. void dv_core_delete(DVMuxContext *c)
  607. {
  608. fifo_free(&c->audio_data);
  609. }