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