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