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