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.

969 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. 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. memset(buf, 0xff, 80);
  356. buf += dv_write_dif_id(dv_sect_audio, i, j/15, buf);
  357. buf += 77; /* audio control & 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. dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3]);
  375. for (d = 8; d < 80; d+=2) {
  376. of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
  377. if (of*2 >= size)
  378. continue;
  379. frame_ptr[d] = pcm[of*2+1]; // FIXME: may be we have to admit
  380. frame_ptr[d+1] = pcm[of*2]; // that DV is a big endian PCM
  381. }
  382. frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  383. }
  384. }
  385. }
  386. static void dv_inject_video(DVMuxContext *c, const uint8_t* video_data, uint8_t* frame_ptr)
  387. {
  388. int i, j;
  389. int ptr = 0;
  390. for (i = 0; i < c->sys->difseg_size; i++) {
  391. ptr += 6 * 80; /* skip DIF segment header */
  392. for (j = 0; j < 135; j++) {
  393. if (j%15 == 0)
  394. ptr += 80; /* skip Audio DIF */
  395. ptr += 3;
  396. memcpy(frame_ptr + ptr, video_data + ptr, 77);
  397. ptr += 77;
  398. }
  399. }
  400. }
  401. /*
  402. * This is the dumbest implementation of all -- it simply looks at
  403. * a fixed offset and if pack isn't there -- fails. We might want
  404. * to have a fallback mechanism for complete search of missing packs.
  405. */
  406. static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
  407. {
  408. int offs;
  409. switch (t) {
  410. case dv_audio_source:
  411. offs = (80*6 + 80*16*3 + 3);
  412. break;
  413. case dv_audio_control:
  414. offs = (80*6 + 80*16*4 + 3);
  415. break;
  416. case dv_video_control:
  417. offs = (80*5 + 48 + 5);
  418. break;
  419. default:
  420. return NULL;
  421. }
  422. return (frame[offs] == t ? &frame[offs] : NULL);
  423. }
  424. /*
  425. * There's a couple of assumptions being made here:
  426. * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
  427. * We can pass them upwards when ffmpeg will be ready to deal with them.
  428. * 2. We don't do software emphasis.
  429. * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
  430. * are converted into 16bit linear ones.
  431. */
  432. static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2)
  433. {
  434. int size, i, j, d, of, smpls, freq, quant, half_ch;
  435. uint16_t lc, rc;
  436. const DVprofile* sys;
  437. const uint8_t* as_pack;
  438. as_pack = dv_extract_pack(frame, dv_audio_source);
  439. if (!as_pack) /* No audio ? */
  440. return 0;
  441. sys = dv_frame_profile(frame);
  442. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  443. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  444. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  445. if (quant > 1)
  446. return -1; /* Unsupported quantization */
  447. size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
  448. half_ch = sys->difseg_size/2;
  449. /* for each DIF segment */
  450. for (i = 0; i < sys->difseg_size; i++) {
  451. frame += 6 * 80; /* skip DIF segment header */
  452. if (quant == 1 && i == half_ch) {
  453. if (!pcm2)
  454. break;
  455. else
  456. pcm = pcm2;
  457. }
  458. for (j = 0; j < 9; j++) {
  459. for (d = 8; d < 80; d += 2) {
  460. if (quant == 0) { /* 16bit quantization */
  461. of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride;
  462. if (of*2 >= size)
  463. continue;
  464. pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit
  465. pcm[of*2+1] = frame[d]; // that DV is a big endian PCM
  466. if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
  467. pcm[of*2+1] = 0;
  468. } else { /* 12bit quantization */
  469. lc = ((uint16_t)frame[d] << 4) |
  470. ((uint16_t)frame[d+2] >> 4);
  471. rc = ((uint16_t)frame[d+1] << 4) |
  472. ((uint16_t)frame[d+2] & 0x0f);
  473. lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
  474. rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
  475. of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride;
  476. if (of*2 >= size)
  477. continue;
  478. pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit
  479. pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM
  480. of = sys->audio_shuffle[i%half_ch+half_ch][j] +
  481. (d - 8)/3 * sys->audio_stride;
  482. pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit
  483. pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM
  484. ++d;
  485. }
  486. }
  487. frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  488. }
  489. }
  490. return size;
  491. }
  492. static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
  493. {
  494. const uint8_t* as_pack;
  495. const DVprofile* sys;
  496. int freq, smpls, quant, i, ach;
  497. sys = dv_frame_profile(frame);
  498. as_pack = dv_extract_pack(frame, dv_audio_source);
  499. if (!as_pack || !sys) { /* No audio ? */
  500. c->ach = 0;
  501. return -1;
  502. }
  503. smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
  504. freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  505. quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
  506. ach = (quant && freq == 2) ? 2 : 1;
  507. /* Dynamic handling of the audio streams in DV */
  508. for (i=0; i<ach; i++) {
  509. if (!c->ast[i]) {
  510. c->ast[i] = av_new_stream(c->fctx, 0);
  511. if (!c->ast[i])
  512. break;
  513. av_set_pts_info(c->ast[i], 64, 1, 30000);
  514. c->ast[i]->codec.codec_type = CODEC_TYPE_AUDIO;
  515. c->ast[i]->codec.codec_id = CODEC_ID_PCM_S16LE;
  516. }
  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. c->ast[i]->start_time = 0;
  521. }
  522. c->ach = i;
  523. return (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
  524. }
  525. static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
  526. {
  527. const DVprofile* sys;
  528. const uint8_t* vsc_pack;
  529. AVCodecContext* avctx;
  530. int apt, is16_9;
  531. int size = 0;
  532. sys = dv_frame_profile(frame);
  533. if (sys) {
  534. avctx = &c->vst->codec;
  535. avctx->frame_rate = sys->frame_rate;
  536. avctx->frame_rate_base = sys->frame_rate_base;
  537. avctx->width = sys->width;
  538. avctx->height = sys->height;
  539. avctx->pix_fmt = sys->pix_fmt;
  540. /* finding out SAR is a little bit messy */
  541. vsc_pack = dv_extract_pack(frame, dv_video_control);
  542. apt = frame[4] & 0x07;
  543. is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
  544. (!apt && (vsc_pack[2] & 0x07) == 0x07)));
  545. avctx->sample_aspect_ratio = sys->sar[is16_9];
  546. size = sys->frame_size;
  547. }
  548. return size;
  549. }
  550. /*
  551. * The following 6 functions constitute our interface to the world
  552. */
  553. int dv_assemble_frame(DVMuxContext *c, AVStream* st,
  554. const uint8_t* data, int data_size, uint8_t** frame)
  555. {
  556. uint8_t pcm[8192];
  557. int fsize, reqasize;
  558. *frame = &c->frame_buf[0];
  559. if (c->has_audio && c->has_video) { /* must be a stale frame */
  560. dv_format_frame(c, *frame);
  561. c->frames++;
  562. if (c->has_audio > 0)
  563. c->has_audio = 0;
  564. c->has_video = 0;
  565. }
  566. if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
  567. /* FIXME: we have to have more sensible approach than this one */
  568. if (c->has_video)
  569. av_log(&st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
  570. dv_inject_video(c, data, *frame);
  571. c->has_video = 1;
  572. data_size = 0;
  573. if (c->has_audio < 0)
  574. goto out;
  575. }
  576. reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
  577. fsize = fifo_size(&c->audio_data, c->audio_data.rptr);
  578. if (st->codec.codec_type == CODEC_TYPE_AUDIO || (c->has_video && fsize >= reqasize)) {
  579. if (fsize + data_size >= reqasize && !c->has_audio) {
  580. if (fsize >= reqasize) {
  581. fifo_read(&c->audio_data, &pcm[0], reqasize, &c->audio_data.rptr);
  582. } else {
  583. fifo_read(&c->audio_data, &pcm[0], fsize, &c->audio_data.rptr);
  584. memcpy(&pcm[fsize], &data[0], reqasize - fsize);
  585. data += reqasize - fsize;
  586. data_size -= reqasize - fsize;
  587. }
  588. dv_inject_audio(c, &pcm[0], *frame);
  589. c->has_audio = 1;
  590. }
  591. /* FIXME: we have to have more sensible approach than this one */
  592. if (fifo_size(&c->audio_data, c->audio_data.rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
  593. av_log(&st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
  594. fifo_write(&c->audio_data, (uint8_t *)data, data_size, &c->audio_data.wptr);
  595. }
  596. out:
  597. return (c->has_audio && c->has_video) ? c->sys->frame_size : 0;
  598. }
  599. DVMuxContext* dv_init_mux(AVFormatContext* s)
  600. {
  601. DVMuxContext *c;
  602. AVStream *vst = NULL;
  603. AVStream *ast = NULL;
  604. int i;
  605. if (s->nb_streams > 2)
  606. return NULL;
  607. c = av_mallocz(sizeof(DVMuxContext));
  608. if (!c)
  609. return NULL;
  610. /* We have to sort out where audio and where video stream is */
  611. for (i=0; i<s->nb_streams; i++) {
  612. switch (s->streams[i]->codec.codec_type) {
  613. case CODEC_TYPE_VIDEO:
  614. vst = s->streams[i];
  615. break;
  616. case CODEC_TYPE_AUDIO:
  617. ast = s->streams[i];
  618. break;
  619. default:
  620. goto bail_out;
  621. }
  622. }
  623. /* Some checks -- DV format is very picky about its incoming streams */
  624. if (!vst || vst->codec.codec_id != CODEC_ID_DVVIDEO)
  625. goto bail_out;
  626. if (ast && (ast->codec.codec_id != CODEC_ID_PCM_S16LE ||
  627. ast->codec.sample_rate != 48000 ||
  628. ast->codec.channels != 2))
  629. goto bail_out;
  630. c->sys = dv_codec_profile(&vst->codec);
  631. if (!c->sys)
  632. goto bail_out;
  633. /* Ok, everything seems to be in working order */
  634. c->frames = 0;
  635. c->has_audio = ast ? 0 : -1;
  636. c->has_video = 0;
  637. c->start_time = (time_t)s->timestamp;
  638. c->aspect = 0; /* 4:3 is the default */
  639. if ((int)(av_q2d(vst->codec.sample_aspect_ratio) * vst->codec.width / vst->codec.height * 10) == 17) /* 16:9 */
  640. c->aspect = 0x07;
  641. if (ast && fifo_init(&c->audio_data, 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0)
  642. goto bail_out;
  643. dv_format_frame(c, &c->frame_buf[0]);
  644. return c;
  645. bail_out:
  646. av_free(c);
  647. return NULL;
  648. }
  649. void dv_delete_mux(DVMuxContext *c)
  650. {
  651. fifo_free(&c->audio_data);
  652. }
  653. DVDemuxContext* dv_init_demux(AVFormatContext *s)
  654. {
  655. DVDemuxContext *c;
  656. c = av_mallocz(sizeof(DVDemuxContext));
  657. if (!c)
  658. return NULL;
  659. c->vst = av_new_stream(s, 0);
  660. if (!c->vst) {
  661. av_free(c);
  662. return NULL;
  663. }
  664. av_set_pts_info(c->vst, 64, 1, 30000);
  665. c->fctx = s;
  666. c->ast[0] = c->ast[1] = NULL;
  667. c->ach = 0;
  668. c->frames = 0;
  669. c->abytes = 0;
  670. c->audio_pkt[0].size = 0;
  671. c->audio_pkt[1].size = 0;
  672. c->vst->codec.codec_type = CODEC_TYPE_VIDEO;
  673. c->vst->codec.codec_id = CODEC_ID_DVVIDEO;
  674. c->vst->codec.bit_rate = 25000000;
  675. c->vst->start_time = 0;
  676. return c;
  677. }
  678. static void __destruct_pkt(struct AVPacket *pkt)
  679. {
  680. pkt->data = NULL; pkt->size = 0;
  681. return;
  682. }
  683. int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
  684. {
  685. int size = -1;
  686. int i;
  687. for (i=0; i<c->ach; i++) {
  688. if (c->ast[i] && c->audio_pkt[i].size) {
  689. *pkt = c->audio_pkt[i];
  690. c->audio_pkt[i].size = 0;
  691. size = pkt->size;
  692. break;
  693. }
  694. }
  695. return size;
  696. }
  697. int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
  698. uint8_t* buf, int buf_size)
  699. {
  700. int size, i;
  701. const DVprofile* sys = dv_frame_profile(buf);
  702. if (buf_size < 4 || buf_size < sys->frame_size)
  703. return -1; /* Broken frame, or not enough data */
  704. /* Queueing audio packet */
  705. /* FIXME: in case of no audio/bad audio we have to do something */
  706. size = dv_extract_audio_info(c, buf);
  707. c->audio_pkt[0].data = c->audio_pkt[1].data = NULL;
  708. for (i=0; i<c->ach; i++) {
  709. if (av_new_packet(&c->audio_pkt[i], size) < 0)
  710. return AVERROR_NOMEM;
  711. c->audio_pkt[i].stream_index = c->ast[i]->index;
  712. c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec.bit_rate;
  713. c->audio_pkt[i].flags |= PKT_FLAG_KEY;
  714. }
  715. dv_extract_audio(buf, c->audio_pkt[0].data, c->audio_pkt[1].data);
  716. c->abytes += size;
  717. /* Now it's time to return video packet */
  718. size = dv_extract_video_info(c, buf);
  719. av_init_packet(pkt);
  720. pkt->destruct = __destruct_pkt;
  721. pkt->data = buf;
  722. pkt->size = size;
  723. pkt->flags |= PKT_FLAG_KEY;
  724. pkt->stream_index = c->vst->id;
  725. pkt->pts = c->frames * sys->frame_rate_base * (30000/sys->frame_rate);
  726. c->frames++;
  727. return size;
  728. }
  729. int64_t dv_frame_offset(DVDemuxContext *c, int64_t timestamp)
  730. {
  731. const DVprofile* sys;
  732. int64_t frame_number, offset;
  733. // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
  734. sys = dv_codec_profile(&c->vst->codec);
  735. // timestamp was scaled by time_base/AV_BASE_RATE by av_seek_frame()
  736. frame_number = (timestamp * sys->frame_rate) /
  737. ((int64_t) 30000 * sys->frame_rate_base);
  738. // offset must be a multiple of frame_size else dv_read_packet() will fail
  739. offset = (int64_t) sys->frame_size * frame_number;
  740. return offset;
  741. }
  742. /************************************************************
  743. * Implementation of the easiest DV storage of all -- raw DV.
  744. ************************************************************/
  745. typedef struct RawDVContext {
  746. uint8_t buf[144000];
  747. DVDemuxContext* dv_demux;
  748. } RawDVContext;
  749. static int dv_read_header(AVFormatContext *s,
  750. AVFormatParameters *ap)
  751. {
  752. RawDVContext *c = s->priv_data;
  753. const DVprofile* sys;
  754. c->dv_demux = dv_init_demux(s);
  755. if (!c->dv_demux)
  756. return -1;
  757. if (get_buffer(&s->pb, c->buf, 4) <= 0 || url_fseek(&s->pb, -4, SEEK_CUR) < 0)
  758. return AVERROR_IO;
  759. sys = dv_frame_profile(c->buf);
  760. s->bit_rate = av_rescale(sys->frame_size * 8, sys->frame_rate, sys->frame_rate_base);
  761. return 0;
  762. }
  763. static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
  764. {
  765. int size;
  766. RawDVContext *c = s->priv_data;
  767. size = dv_get_packet(c->dv_demux, pkt);
  768. if (size < 0) {
  769. if (get_buffer(&s->pb, c->buf, 4) <= 0)
  770. return AVERROR_IO;
  771. size = dv_frame_profile(c->buf)->frame_size;
  772. if (get_buffer(&s->pb, c->buf + 4, size - 4) <= 0)
  773. return AVERROR_IO;
  774. size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
  775. }
  776. return size;
  777. }
  778. static int dv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp)
  779. {
  780. RawDVContext *c = s->priv_data;
  781. return url_fseek(&s->pb, dv_frame_offset(c->dv_demux, timestamp), SEEK_SET);
  782. }
  783. static int dv_read_close(AVFormatContext *s)
  784. {
  785. RawDVContext *c = s->priv_data;
  786. av_free(c->dv_demux);
  787. return 0;
  788. }
  789. static int dv_write_header(AVFormatContext *s)
  790. {
  791. s->priv_data = dv_init_mux(s);
  792. if (!s->priv_data) {
  793. av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
  794. "Make sure that you supply exactly two streams:\n"
  795. " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n");
  796. return -1;
  797. }
  798. return 0;
  799. }
  800. static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  801. {
  802. uint8_t* frame;
  803. int fsize;
  804. fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index],
  805. pkt->data, pkt->size, &frame);
  806. if (fsize > 0) {
  807. put_buffer(&s->pb, frame, fsize);
  808. put_flush_packet(&s->pb);
  809. }
  810. return 0;
  811. }
  812. /*
  813. * We might end up with some extra A/V data without matching counterpart.
  814. * E.g. video data without enough audio to write the complete frame.
  815. * Currently we simply drop the last frame. I don't know whether this
  816. * is the best strategy of all
  817. */
  818. static int dv_write_trailer(struct AVFormatContext *s)
  819. {
  820. dv_delete_mux((DVMuxContext *)s->priv_data);
  821. return 0;
  822. }
  823. static AVInputFormat dv_iformat = {
  824. "dv",
  825. "DV video format",
  826. sizeof(RawDVContext),
  827. NULL,
  828. dv_read_header,
  829. dv_read_packet,
  830. dv_read_close,
  831. dv_read_seek,
  832. .extensions = "dv,dif",
  833. };
  834. static AVOutputFormat dv_oformat = {
  835. "dv",
  836. "DV video format",
  837. NULL,
  838. "dv",
  839. sizeof(DVMuxContext),
  840. CODEC_ID_PCM_S16LE,
  841. CODEC_ID_DVVIDEO,
  842. dv_write_header,
  843. dv_write_packet,
  844. dv_write_trailer,
  845. };
  846. int ff_dv_init(void)
  847. {
  848. av_register_input_format(&dv_iformat);
  849. av_register_output_format(&dv_oformat);
  850. return 0;
  851. }