1 /*
2 * General DV muxer/demuxer
3 * Copyright (c) 2003 Roman Shaposhnik
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 * 50 Mbps (DVCPRO50) support
12 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
13 *
14 * This file is part of FFmpeg.
15 *
16 * FFmpeg is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
20 *
21 * FFmpeg is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with FFmpeg; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 */
31 #include <stdarg.h>
32
43
44 #define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
45
48 const DVprofile*
sys;
/* current DV profile, e.g.: 525/60, 625/50 */
49 int n_ast;
/* number of stereo audio streams (up to 2) */
52 int frames;
/* current frame number */
54 int has_audio;
/* frame under construction has audio */
55 int has_video;
/* frame under construction has video */
58 };
59
61 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
62 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
63 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
64 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
65 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
66 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
67 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
68 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
69 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
70 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
71 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
72 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
73 };
74
76 {
79 }
80
82 {
83 struct tm tc;
84 time_t ct;
85 uint32_t timecode;
86 va_list ap;
87
89 switch (pack_id) {
92 timecode |= 1<<23 | 1<<15 | 1<<7 | 1<<6; // biphase and binary group flags
94 break;
96 va_start(ap, buf);
97 buf[1] = (1 << 7) | /* locked mode -- SMPTE only supports locked mode */
98 (1 << 6) | /* reserved -- always 1 */
101 /* # of samples */
102 buf[2] = (0 << 7) | /* multi-stereo */
103 (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
104 (0 << 4) | /* pair bit: 0 -- one pair of channels */
105 !!va_arg(ap, int); /* audio mode */
106 buf[3] = (1 << 7) | /* res */
107 (1 << 6) | /* multi-language flag */
108 (c->
sys->
dsf << 5) |
/* system: 60fields/50fields */
109 (c->
sys->
n_difchan & 2);
/* definition: 0 -- 25Mbps, 2 -- 50Mbps */
110 buf[4] = (1 << 7) | /* emphasis: 1 -- off */
111 (0 << 6) | /* emphasis time constant: 0 -- reserved */
112 (0 << 3) | /* frequency: 0 -- 48kHz, 1 -- 44,1kHz, 2 -- 32kHz */
113 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
114 va_end(ap);
115 break;
117 buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
118 (1 << 4) | /* input source: 1 -- digital input */
119 (3 << 2) | /* compression: 3 -- no information */
120 0; /* misc. info/SMPTE emphasis off */
121 buf[2] = (1 << 7) | /* recording start point: 1 -- no */
122 (1 << 6) | /* recording end point: 1 -- no */
123 (1 << 3) | /* recording mode: 1 -- original */
124 7;
125 buf[3] = (1 << 7) | /* direction: 1 -- forward */
128 buf[4] = (1 << 7) | /* reserved -- always 1 */
129 0x7f; /* genre category */
130 break;
136 buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
137 /* 0xff is very likely to be "unknown" */
138 buf[2] = (3 << 6) | /* reserved -- always 1 */
139 ((tc.tm_mday / 10) << 4) | /* Tens of day */
140 (tc.tm_mday % 10); /* Units of day */
141 buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
142 ((tc.tm_mon / 10) << 4) | /* Tens of month */
143 (tc.tm_mon % 10); /* Units of month */
144 buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
145 (tc.tm_year % 10); /* Units of year */
146 break;
152 buf[1] = (3 << 6) | /* reserved -- always 1 */
153 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
154 buf[2] = (1 << 7) | /* reserved -- always 1 */
155 ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
156 (tc.tm_sec % 10); /* Units of seconds */
157 buf[3] = (1 << 7) | /* reserved -- always 1 */
158 ((tc.tm_min / 10) << 4) | /* Tens of minutes */
159 (tc.tm_min % 10); /* Units of minutes */
160 buf[4] = (3 << 6) | /* reserved -- always 1 */
161 ((tc.tm_hour / 10) << 4) | /* Tens of hours */
162 (tc.tm_hour % 10); /* Units of hours */
163 break;
164 default:
165 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
166 }
167 return 5;
168 }
169
171 {
172 int i, j, d, of,
size;
176 frame_ptr += 6 * 80; /* skip DIF segment header */
177 for (j = 0; j < 9; j++) {
179 for (d = 8; d < 80; d+=2) {
182 continue;
183
186 }
187 frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
188 }
189 }
190 }
191
193 {
194 int j, k;
196
197 for (buf = frame; buf < frame + c->
sys->
frame_size; buf += 150 * 80) {
198 /* DV subcode: 2nd and 3d DIFs */
199 for (j = 80; j < 80 * 3; j += 80) {
200 for (k = 6; k < 6 * 8; k += 8)
202
208 }
209 }
210
211 /* DV VAUX: 4th, 5th and 6th 3DIFs */
212 for (j = 80*3 + 3; j < 80*6; j += 80) {
217 }
218 }
219 }
220
221 /*
222 * The following 3 functions constitute our interface to the world
223 */
224
227 {
228 int i, reqasize;
229
232
235 /* FIXME: we have to have more sensible approach than this one */
238
241 break;
243 for (i = 0; i < c->
n_ast && st != c->
ast[i]; i++);
244
245 /* FIXME: we have to have more sensible approach than this one */
249
250 /* Let us see if we've got enough audio for one DV frame. */
252
253 break;
254 default:
255 break;
256 }
257
258 /* Let us see if we have enough data to construct one DV frame. */
262 for (i=0; i < c->
n_ast; i++) {
266 }
267
269
271
273 }
274
275 return 0;
276 }
277
279 {
283 int i;
284
285 /* we support at most 1 video and 2 audio streams */
287 return NULL;
288
290 c->
ast[0] = c->
ast[1] = NULL;
291
292 /* We have to sort out where audio and where video stream is */
296 if (vst) return NULL;
298 break;
300 if (c->
n_ast > 1)
return NULL;
302 break;
303 default:
304 goto bail_out;
305 }
306 }
307
308 /* Some checks -- DV format is very picky about its incoming streams */
310 goto bail_out;
311 for (i=0; i<c->
n_ast; i++) {
315 goto bail_out;
316 }
319 goto bail_out;
320
322 /* only 1 stereo pair is allowed in 25Mbps mode */
323 goto bail_out;
324 }
325
326 /* Ok, everything seems to be in working order */
332
333 for (i=0; i < c->
n_ast; i++) {
335 while (i > 0) {
336 i--;
338 }
339 goto bail_out;
340 }
341 }
342
344
345 bail_out:
346 return NULL;
347 }
348
350 {
351 int i;
352 for (i=0; i < c->
n_ast; i++)
354 }
355
357 {
361
364 "Make sure that you supply exactly two streams:\n"
365 " video: 25fps or 29.97fps, audio: 2ch/48kHz/PCM\n"
366 " (50Mbps allows an optional second audio stream)\n");
367 return -1;
368 }
371 if (!tcr) { // no global timecode, look into the streams
372 int i;
375 if (tcr)
376 break;
377 }
378 }
380 return 0;
382 }
383
385 {
387 int fsize;
388
391 if (fsize > 0) {
393 }
394 return 0;
395 }
396
397 /*
398 * We might end up with some extra A/V data without matching counterpart.
399 * E.g. video data without enough audio to write the complete frame.
400 * Currently we simply drop the last frame. I don't know whether this
401 * is the best strategy of all
402 */
404 {
406 return 0;
407 }
408
412 .extensions = "dv",
419 };