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.

946 lines
32KB

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