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.

974 lines
33KB

  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. brktimegm(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. brktimegm(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) << 4) | /* Tens of month */
  247. (tc.tm_mon % 10); /* Units of month */
  248. buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
  249. (tc.tm_year % 10); /* Units of year */
  250. break;
  251. case dv_audio_rectime: /* AAUX recording time */
  252. case dv_video_rectime: /* VAUX recording time */
  253. ct = c->start_time + (time_t)(c->frames /
  254. ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
  255. brktimegm(ct, &tc);
  256. buf[1] = (3 << 6) | /* reserved -- always 1 */
  257. 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
  258. buf[2] = (1 << 7) | /* reserved -- always 1 */
  259. ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
  260. (tc.tm_sec % 10); /* Units of seconds */
  261. buf[3] = (1 << 7) | /* reserved -- always 1 */
  262. ((tc.tm_min / 10) << 4) | /* Tens of minutes */
  263. (tc.tm_min % 10); /* Units of minutes */
  264. buf[4] = (3 << 6) | /* reserved -- always 1 */
  265. ((tc.tm_hour / 10) << 4) | /* Tens of hours */
  266. (tc.tm_hour % 10); /* Units of hours */
  267. break;
  268. case dv_video_source:
  269. buf[1] = 0xff; /* reserved -- always 1 */
  270. buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
  271. (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
  272. (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
  273. 0xf; /* reserved -- always 1 */
  274. buf[3] = (3 << 6) | /* reserved -- always 1 */
  275. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  276. 0; /* signal type video compression */
  277. buf[4] = 0xff; /* VISC: 0xff -- no information */
  278. break;
  279. case dv_video_control:
  280. buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
  281. 0x3f; /* reserved -- always 1 */
  282. buf[2] = 0xc8 | /* reserved -- always b11001xxx */
  283. c->aspect;
  284. buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */
  285. (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */
  286. (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */
  287. (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
  288. 0xc; /* reserved -- always b1100 */
  289. buf[4] = 0xff; /* reserved -- always 1 */
  290. break;
  291. default:
  292. buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
  293. }
  294. return 5;
  295. }
  296. static inline int dv_write_dif_id(enum dv_section_type t, uint8_t seq_num,
  297. uint8_t dif_num, uint8_t* buf)
  298. {
  299. buf[0] = (uint8_t)t; /* Section type */
  300. buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
  301. (0 << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
  302. 7; /* reserved -- always 1 */
  303. buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
  304. return 3;
  305. }
  306. static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
  307. {
  308. if (syb_num == 0 || syb_num == 6) {
  309. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  310. (0<<4) | /* AP3 (Subcode application ID) */
  311. 0x0f; /* reserved -- always 1 */
  312. }
  313. else if (syb_num == 11) {
  314. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  315. 0x7f; /* reserved -- always 1 */
  316. }
  317. else {
  318. buf[0] = (fr<<7) | /* FR ID 1 - first half of each channel; 0 - second */
  319. (0<<4) | /* APT (Track application ID) */
  320. 0x0f; /* reserved -- always 1 */
  321. }
  322. buf[1] = 0xf0 | /* reserved -- always 1 */
  323. (syb_num & 0x0f); /* SSYB number 0 - 11 */
  324. buf[2] = 0xff; /* reserved -- always 1 */
  325. return 3;
  326. }
  327. static void dv_format_frame(DVMuxContext *c, uint8_t* buf)
  328. {
  329. int i, j, k;
  330. for (i = 0; i < c->sys->difseg_size; i++) {
  331. memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
  332. /* DV header: 1DIF */
  333. buf += dv_write_dif_id(dv_sect_header, i, 0, buf);
  334. buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
  335. buf += 72; /* unused bytes */
  336. /* DV subcode: 2DIFs */
  337. for (j = 0; j < 2; j++) {
  338. buf += dv_write_dif_id( dv_sect_subcode, i, j, buf);
  339. for (k = 0; k < 6; k++) {
  340. buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf);
  341. buf += dv_write_pack(dv_ssyb_packs_dist[i][k], c, buf);
  342. }
  343. buf += 29; /* unused bytes */
  344. }
  345. /* DV VAUX: 3DIFs */
  346. for (j = 0; j < 3; j++) {
  347. buf += dv_write_dif_id(dv_sect_vaux, i, j, buf);
  348. for (k = 0; k < 15 ; k++)
  349. buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf);
  350. buf += 2; /* unused bytes */
  351. }
  352. /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
  353. for (j = 0; j < 135; j++) {
  354. if (j%15 == 0) {
  355. buf += dv_write_dif_id(dv_sect_audio, i, j/15, buf);
  356. buf += dv_write_pack(dv_aaux_packs_dist[i][j/15], c, buf);
  357. buf += 72; /* shuffled PCM audio */
  358. }
  359. buf += dv_write_dif_id(dv_sect_video, i, j, buf);
  360. buf += 77; /* 1 video macro block: 1 bytes control
  361. 4 * 14 bytes Y 8x8 data
  362. 10 bytes Cr 8x8 data
  363. 10 bytes Cb 8x8 data */
  364. }
  365. }
  366. }
  367. static void dv_inject_audio(DVMuxContext *c, const uint8_t* pcm, uint8_t* frame_ptr)
  368. {
  369. int i, j, d, of, size;
  370. size = 4 * dv_audio_frame_size(c->sys, c->frames);
  371. for (i = 0; i < c->sys->difseg_size; i++) {
  372. frame_ptr += 6 * 80; /* skip DIF segment header */
  373. for (j = 0; j < 9; j++) {
  374. for (d = 8; d < 80; d+=2) {
  375. of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
  376. if (of*2 >= size)
  377. continue;
  378. frame_ptr[d] = pcm[of*2+1]; // FIXME: may be we have to admit
  379. frame_ptr[d+1] = pcm[of*2]; // that DV is a big endian PCM
  380. }
  381. frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  382. }
  383. }
  384. }
  385. static void dv_inject_video(DVMuxContext *c, const uint8_t* video_data, uint8_t* frame_ptr)
  386. {
  387. int i, j;
  388. int ptr = 0;
  389. for (i = 0; i < c->sys->difseg_size; i++) {
  390. ptr += 6 * 80; /* skip DIF segment header */
  391. for (j = 0; j < 135; j++) {
  392. if (j%15 == 0)
  393. ptr += 80; /* skip Audio DIF */
  394. ptr += 3;
  395. memcpy(frame_ptr + ptr, video_data + ptr, 77);
  396. ptr += 77;
  397. }
  398. }
  399. }
  400. /*
  401. * This is the dumbest implementation of all -- it simply looks at
  402. * a fixed offset and if pack isn't there -- fails. We might want
  403. * to have a fallback mechanism for complete search of missing packs.
  404. */
  405. static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
  406. {
  407. int offs;
  408. switch (t) {
  409. case dv_audio_source:
  410. offs = (80*6 + 80*16*3 + 3);
  411. break;
  412. case dv_audio_control:
  413. offs = (80*6 + 80*16*4 + 3);
  414. break;
  415. case dv_video_control:
  416. offs = (80*5 + 48 + 5);
  417. break;
  418. default:
  419. return NULL;
  420. }
  421. return (frame[offs] == t ? &frame[offs] : NULL);
  422. }
  423. /*
  424. * There's a couple of assumptions being made here:
  425. * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
  426. * We can pass them upwards when ffmpeg will be ready to deal with them.
  427. * 2. We don't do software emphasis.
  428. * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
  429. * are converted into 16bit linear ones.
  430. */
  431. static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2)
  432. {
  433. int size, i, j, d, of, smpls, freq, quant, half_ch;
  434. uint16_t lc, rc;
  435. const DVprofile* sys;
  436. const uint8_t* as_pack;
  437. as_pack = dv_extract_pack(frame, dv_audio_source);
  438. if (!as_pack) /* No audio ? */
  439. return 0;
  440. sys = dv_frame_profile(frame);
  441. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  442. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  443. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  444. if (quant > 1)
  445. return -1; /* Unsupported quantization */
  446. size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
  447. half_ch = sys->difseg_size/2;
  448. /* for each DIF segment */
  449. for (i = 0; i < sys->difseg_size; i++) {
  450. frame += 6 * 80; /* skip DIF segment header */
  451. if (quant == 1 && i == half_ch) {
  452. if (!pcm2)
  453. break;
  454. else
  455. pcm = pcm2;
  456. }
  457. for (j = 0; j < 9; j++) {
  458. for (d = 8; d < 80; d += 2) {
  459. if (quant == 0) { /* 16bit quantization */
  460. of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride;
  461. if (of*2 >= size)
  462. continue;
  463. pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit
  464. pcm[of*2+1] = frame[d]; // that DV is a big endian PCM
  465. if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
  466. pcm[of*2+1] = 0;
  467. } else { /* 12bit quantization */
  468. lc = ((uint16_t)frame[d] << 4) |
  469. ((uint16_t)frame[d+2] >> 4);
  470. rc = ((uint16_t)frame[d+1] << 4) |
  471. ((uint16_t)frame[d+2] & 0x0f);
  472. lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
  473. rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
  474. of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride;
  475. if (of*2 >= size)
  476. continue;
  477. pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit
  478. pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM
  479. of = sys->audio_shuffle[i%half_ch+half_ch][j] +
  480. (d - 8)/3 * sys->audio_stride;
  481. pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit
  482. pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM
  483. ++d;
  484. }
  485. }
  486. frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  487. }
  488. }
  489. return size;
  490. }
  491. static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
  492. {
  493. const uint8_t* as_pack;
  494. const DVprofile* sys;
  495. int freq, smpls, quant, i;
  496. sys = dv_frame_profile(frame);
  497. as_pack = dv_extract_pack(frame, dv_audio_source);
  498. if (!as_pack || !sys) { /* No audio ? */
  499. c->ach = 0;
  500. return -1;
  501. }
  502. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  503. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  504. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  505. c->ach = (quant && freq == 2) ? 2 : 1;
  506. /* The second stereo channel could appear in IEC 61834 stream only */
  507. if (c->ach == 2 && !c->ast[1]) {
  508. c->ast[1] = av_new_stream(c->fctx, 0);
  509. if (c->ast[1]) {
  510. av_set_pts_info(c->ast[1], 64, 1, 30000);
  511. c->ast[1]->codec.codec_type = CODEC_TYPE_AUDIO;
  512. c->ast[1]->codec.codec_id = CODEC_ID_PCM_S16LE;
  513. } else
  514. c->ach = 1;
  515. }
  516. for (i=0; i<c->ach; i++) {
  517. c->ast[i]->codec.sample_rate = dv_audio_frequency[freq];
  518. c->ast[i]->codec.channels = 2;
  519. c->ast[i]->codec.bit_rate = 2 * dv_audio_frequency[freq] * 16;
  520. }
  521. return (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
  522. }
  523. static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
  524. {
  525. const DVprofile* sys;
  526. const uint8_t* vsc_pack;
  527. AVCodecContext* avctx;
  528. int apt, is16_9;
  529. int size = 0;
  530. sys = dv_frame_profile(frame);
  531. if (sys) {
  532. avctx = &c->vst->codec;
  533. avctx->frame_rate = sys->frame_rate;
  534. avctx->frame_rate_base = sys->frame_rate_base;
  535. avctx->width = sys->width;
  536. avctx->height = sys->height;
  537. avctx->pix_fmt = sys->pix_fmt;
  538. /* finding out SAR is a little bit messy */
  539. vsc_pack = dv_extract_pack(frame, dv_video_control);
  540. apt = frame[4] & 0x07;
  541. is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
  542. (!apt && (vsc_pack[2] & 0x07) == 0x07)));
  543. avctx->sample_aspect_ratio = sys->sar[is16_9];
  544. size = sys->frame_size;
  545. }
  546. return size;
  547. }
  548. /*
  549. * The following 6 functions constitute our interface to the world
  550. */
  551. int dv_assemble_frame(DVMuxContext *c, AVStream* st,
  552. const uint8_t* data, int data_size, uint8_t** frame)
  553. {
  554. uint8_t pcm[8192];
  555. int fsize, reqasize;
  556. *frame = &c->frame_buf[0];
  557. if (c->has_audio && c->has_video) { /* must be a stale frame */
  558. dv_format_frame(c, *frame);
  559. c->frames++;
  560. c->has_audio = c->has_video = 0;
  561. }
  562. if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
  563. /* FIXME: we have to have more sensible approach than this one */
  564. if (c->has_video)
  565. av_log(&st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
  566. dv_inject_video(c, data, *frame);
  567. c->has_video = 1;
  568. data_size = 0;
  569. }
  570. reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
  571. fsize = fifo_size(&c->audio_data, c->audio_data.rptr);
  572. if (st->codec.codec_type == CODEC_TYPE_AUDIO || (c->has_video && fsize >= reqasize)) {
  573. if (fsize + data_size >= reqasize && !c->has_audio) {
  574. if (fsize >= reqasize) {
  575. fifo_read(&c->audio_data, &pcm[0], reqasize, &c->audio_data.rptr);
  576. } else {
  577. fifo_read(&c->audio_data, &pcm[0], fsize, &c->audio_data.rptr);
  578. memcpy(&pcm[fsize], &data[0], reqasize - fsize);
  579. data += reqasize - fsize;
  580. data_size -= reqasize - fsize;
  581. }
  582. dv_inject_audio(c, &pcm[0], *frame);
  583. c->has_audio = 1;
  584. }
  585. /* FIXME: we have to have more sensible approach than this one */
  586. if (fifo_size(&c->audio_data, c->audio_data.rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
  587. av_log(&st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
  588. fifo_write(&c->audio_data, (uint8_t *)data, data_size, &c->audio_data.wptr);
  589. }
  590. return (c->has_audio && c->has_video) ? c->sys->frame_size : 0;
  591. }
  592. DVMuxContext* dv_init_mux(AVFormatContext* s)
  593. {
  594. DVMuxContext *c;
  595. AVStream *vst;
  596. AVStream *ast;
  597. c = av_mallocz(sizeof(DVMuxContext));
  598. if (!c)
  599. return NULL;
  600. if (s->nb_streams != 2)
  601. goto bail_out;
  602. /* We have to sort out where audio and where video stream is */
  603. if (s->streams[0]->codec.codec_type == CODEC_TYPE_VIDEO &&
  604. s->streams[1]->codec.codec_type == CODEC_TYPE_AUDIO) {
  605. vst = s->streams[0];
  606. ast = s->streams[1];
  607. }
  608. else if (s->streams[1]->codec.codec_type == CODEC_TYPE_VIDEO &&
  609. s->streams[0]->codec.codec_type == CODEC_TYPE_AUDIO) {
  610. vst = s->streams[1];
  611. ast = s->streams[0];
  612. } else
  613. goto bail_out;
  614. /* Some checks -- DV format is very picky about its incoming streams */
  615. if (vst->codec.codec_id != CODEC_ID_DVVIDEO ||
  616. ast->codec.codec_id != CODEC_ID_PCM_S16LE)
  617. goto bail_out;
  618. if (ast->codec.sample_rate != 48000 ||
  619. ast->codec.channels != 2)
  620. goto bail_out;
  621. c->sys = dv_codec_profile(&vst->codec);
  622. if (!c->sys)
  623. goto bail_out;
  624. /* Ok, everything seems to be in working order */
  625. c->frames = 0;
  626. c->has_audio = c->has_video = 0;
  627. c->start_time = (time_t)s->timestamp;
  628. c->aspect = 0; /* 4:3 is the default */
  629. if ((int)(av_q2d(vst->codec.sample_aspect_ratio) * vst->codec.width / vst->codec.height * 10) == 17) /* 16:9 */
  630. c->aspect = 0x07;
  631. if (fifo_init(&c->audio_data, 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0)
  632. goto bail_out;
  633. dv_format_frame(c, &c->frame_buf[0]);
  634. return c;
  635. bail_out:
  636. av_free(c);
  637. return NULL;
  638. }
  639. void dv_delete_mux(DVMuxContext *c)
  640. {
  641. fifo_free(&c->audio_data);
  642. }
  643. DVDemuxContext* dv_init_demux(AVFormatContext *s)
  644. {
  645. DVDemuxContext *c;
  646. c = av_mallocz(sizeof(DVDemuxContext));
  647. if (!c)
  648. return NULL;
  649. c->vst = av_new_stream(s, 0);
  650. c->ast[0] = av_new_stream(s, 0);
  651. if (!c->vst || !c->ast[0])
  652. goto fail;
  653. av_set_pts_info(c->vst, 64, 1, 30000);
  654. av_set_pts_info(c->ast[0], 64, 1, 30000);
  655. c->fctx = s;
  656. c->ast[1] = NULL;
  657. c->ach = 0;
  658. c->frames = 0;
  659. c->abytes = 0;
  660. c->audio_pkt[0].size = 0;
  661. c->audio_pkt[1].size = 0;
  662. c->vst->codec.codec_type = CODEC_TYPE_VIDEO;
  663. c->vst->codec.codec_id = CODEC_ID_DVVIDEO;
  664. c->vst->codec.bit_rate = 25000000;
  665. c->vst->start_time = 0;
  666. c->ast[0]->codec.codec_type = CODEC_TYPE_AUDIO;
  667. c->ast[0]->codec.codec_id = CODEC_ID_PCM_S16LE;
  668. c->ast[0]->codec.sample_rate = 48000;
  669. c->ast[0]->codec.channels = 2;
  670. c->ast[0]->start_time = 0;
  671. return c;
  672. fail:
  673. if (c->vst)
  674. av_free(c->vst);
  675. if (c->ast[0])
  676. av_free(c->ast[0]);
  677. av_free(c);
  678. return NULL;
  679. }
  680. static void __destruct_pkt(struct AVPacket *pkt)
  681. {
  682. pkt->data = NULL; pkt->size = 0;
  683. return;
  684. }
  685. int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
  686. {
  687. int size = -1;
  688. int i;
  689. for (i=0; i<c->ach; i++) {
  690. if (c->ast[i] && c->audio_pkt[i].size) {
  691. *pkt = c->audio_pkt[i];
  692. c->audio_pkt[i].size = 0;
  693. size = pkt->size;
  694. break;
  695. }
  696. }
  697. return size;
  698. }
  699. int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
  700. uint8_t* buf, int buf_size)
  701. {
  702. int size, i;
  703. const DVprofile* sys = dv_frame_profile(buf);
  704. if (buf_size < 4 || buf_size < sys->frame_size)
  705. return -1; /* Broken frame, or not enough data */
  706. /* Queueing audio packet */
  707. /* FIXME: in case of no audio/bad audio we have to do something */
  708. size = dv_extract_audio_info(c, buf);
  709. c->audio_pkt[0].data = c->audio_pkt[1].data = NULL;
  710. for (i=0; i<c->ach; i++) {
  711. if (av_new_packet(&c->audio_pkt[i], size) < 0)
  712. return AVERROR_NOMEM;
  713. c->audio_pkt[i].stream_index = c->ast[i]->index;
  714. c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec.bit_rate;
  715. c->audio_pkt[i].flags |= PKT_FLAG_KEY;
  716. }
  717. dv_extract_audio(buf, c->audio_pkt[0].data, c->audio_pkt[1].data);
  718. c->abytes += size;
  719. /* Now it's time to return video packet */
  720. size = dv_extract_video_info(c, buf);
  721. av_init_packet(pkt);
  722. pkt->destruct = __destruct_pkt;
  723. pkt->data = buf;
  724. pkt->size = size;
  725. pkt->flags |= PKT_FLAG_KEY;
  726. pkt->stream_index = c->vst->id;
  727. pkt->pts = c->frames * sys->frame_rate_base * (30000/sys->frame_rate);
  728. c->frames++;
  729. return size;
  730. }
  731. int64_t dv_frame_offset(DVDemuxContext *c, int64_t timestamp)
  732. {
  733. const DVprofile* sys;
  734. int64_t frame_number, offset;
  735. // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
  736. sys = dv_codec_profile(&c->vst->codec);
  737. // timestamp was scaled by time_base/AV_BASE_RATE by av_seek_frame()
  738. frame_number = (timestamp * sys->frame_rate) /
  739. ((int64_t) 30000 * sys->frame_rate_base);
  740. // offset must be a multiple of frame_size else dv_read_packet() will fail
  741. offset = (int64_t) sys->frame_size * frame_number;
  742. return offset;
  743. }
  744. /************************************************************
  745. * Implementation of the easiest DV storage of all -- raw DV.
  746. ************************************************************/
  747. typedef struct RawDVContext {
  748. uint8_t buf[144000];
  749. DVDemuxContext* dv_demux;
  750. } RawDVContext;
  751. static int dv_read_header(AVFormatContext *s,
  752. AVFormatParameters *ap)
  753. {
  754. RawDVContext *c = s->priv_data;
  755. const DVprofile* sys;
  756. c->dv_demux = dv_init_demux(s);
  757. if (!c->dv_demux)
  758. return -1;
  759. if (get_buffer(&s->pb, c->buf, 4) <= 0 || url_fseek(&s->pb, -4, SEEK_CUR) < 0)
  760. return AVERROR_IO;
  761. sys = dv_frame_profile(c->buf);
  762. s->bit_rate = av_rescale(sys->frame_size * 8, sys->frame_rate, sys->frame_rate_base);
  763. return 0;
  764. }
  765. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  766. {
  767. int size;
  768. RawDVContext *c = s->priv_data;
  769. size = dv_get_packet(c->dv_demux, pkt);
  770. if (size < 0) {
  771. if (get_buffer(&s->pb, c->buf, 4) <= 0)
  772. return AVERROR_IO;
  773. size = dv_frame_profile(c->buf)->frame_size;
  774. if (get_buffer(&s->pb, c->buf + 4, size - 4) <= 0)
  775. return AVERROR_IO;
  776. size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
  777. }
  778. return size;
  779. }
  780. static int dv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp)
  781. {
  782. RawDVContext *c = s->priv_data;
  783. return url_fseek(&s->pb, dv_frame_offset(c->dv_demux, timestamp), SEEK_SET);
  784. }
  785. static int dv_read_close(AVFormatContext *s)
  786. {
  787. RawDVContext *c = s->priv_data;
  788. av_free(c->dv_demux);
  789. return 0;
  790. }
  791. static int dv_write_header(AVFormatContext *s)
  792. {
  793. s->priv_data = dv_init_mux(s);
  794. if (!s->priv_data) {
  795. av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
  796. "Make sure that you supply exactly two streams:\n"
  797. " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n");
  798. return -1;
  799. }
  800. return 0;
  801. }
  802. static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  803. {
  804. uint8_t* frame;
  805. int fsize;
  806. fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index],
  807. pkt->data, pkt->size, &frame);
  808. if (fsize > 0) {
  809. put_buffer(&s->pb, frame, fsize);
  810. put_flush_packet(&s->pb);
  811. }
  812. return 0;
  813. }
  814. /*
  815. * We might end up with some extra A/V data without matching counterpart.
  816. * E.g. video data without enough audio to write the complete frame.
  817. * Currently we simply drop the last frame. I don't know whether this
  818. * is the best strategy of all
  819. */
  820. static int dv_write_trailer(struct AVFormatContext *s)
  821. {
  822. dv_delete_mux((DVMuxContext *)s->priv_data);
  823. return 0;
  824. }
  825. static AVInputFormat dv_iformat = {
  826. "dv",
  827. "DV video format",
  828. sizeof(RawDVContext),
  829. NULL,
  830. dv_read_header,
  831. dv_read_packet,
  832. dv_read_close,
  833. dv_read_seek,
  834. .extensions = "dv,dif",
  835. };
  836. static AVOutputFormat dv_oformat = {
  837. "dv",
  838. "DV video format",
  839. NULL,
  840. "dv",
  841. sizeof(DVMuxContext),
  842. CODEC_ID_PCM_S16LE,
  843. CODEC_ID_DVVIDEO,
  844. dv_write_header,
  845. dv_write_packet,
  846. dv_write_trailer,
  847. };
  848. int ff_dv_init(void)
  849. {
  850. av_register_input_format(&dv_iformat);
  851. av_register_output_format(&dv_oformat);
  852. return 0;
  853. }