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.

881 lines
30KB

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