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.

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