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.

917 lines
31KB

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