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.

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