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
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 {
78 if (sample_rate == 32000) return 1280;
79 else if (sample_rate == 44100) return 1764;
80 else return 1920;
81 }
82
84
87 }
88
90 {
91 struct tm tc;
92 time_t ct;
93 uint32_t timecode;
94 va_list ap;
95 int audio_type = 0;
96 int channel;
97
99 switch (pack_id) {
102 timecode |= 1<<23 | 1<<15 | 1<<7 | 1<<6; // biphase and binary group flags
104 break;
106 va_start(ap, buf);
107 channel = va_arg(ap, int);
109 audio_type = 1;
111 audio_type = 2;
112 buf[1] = (1 << 7) | /* locked mode -- SMPTE only supports locked mode */
113 (1 << 6) | /* reserved -- always 1 */
116 /* # of samples */
117 buf[2] = (0 << 7) | /* multi-stereo */
118 (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
119 (0 << 4) | /* pair bit: 0 -- one pair of channels */
120 !!va_arg(ap, int); /* audio mode */
121 buf[3] = (1 << 7) | /* res */
122 (1 << 6) | /* multi-language flag */
123 (c->
sys->
dsf << 5) |
/* system: 60fields/50fields */
124 (c->
sys->
n_difchan & 2);
/* definition: 0 -- 25Mbps, 2 -- 50Mbps */
125 buf[4] = (1 << 7) | /* emphasis: 1 -- off */
126 (0 << 6) | /* emphasis time constant: 0 -- reserved */
127 (audio_type << 3) | /* frequency: 0 -- 48kHz, 1 -- 44,1kHz, 2 -- 32kHz */
128 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
129
130 va_end(ap);
131 break;
133 buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
134 (1 << 4) | /* input source: 1 -- digital input */
135 (3 << 2) | /* compression: 3 -- no information */
136 0; /* misc. info/SMPTE emphasis off */
137 buf[2] = (1 << 7) | /* recording start point: 1 -- no */
138 (1 << 6) | /* recording end point: 1 -- no */
139 (1 << 3) | /* recording mode: 1 -- original */
140 7;
141 buf[3] = (1 << 7) | /* direction: 1 -- forward */
144 buf[4] = (1 << 7) | /* reserved -- always 1 */
145 0x7f; /* genre category */
146 break;
152 buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
153 /* 0xff is very likely to be "unknown" */
154 buf[2] = (3 << 6) | /* reserved -- always 1 */
155 ((tc.tm_mday / 10) << 4) | /* Tens of day */
156 (tc.tm_mday % 10); /* Units of day */
157 buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
158 ((tc.tm_mon / 10) << 4) | /* Tens of month */
159 (tc.tm_mon % 10); /* Units of month */
160 buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
161 (tc.tm_year % 10); /* Units of year */
162 break;
168 buf[1] = (3 << 6) | /* reserved -- always 1 */
169 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
170 buf[2] = (1 << 7) | /* reserved -- always 1 */
171 ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
172 (tc.tm_sec % 10); /* Units of seconds */
173 buf[3] = (1 << 7) | /* reserved -- always 1 */
174 ((tc.tm_min / 10) << 4) | /* Tens of minutes */
175 (tc.tm_min % 10); /* Units of minutes */
176 buf[4] = (3 << 6) | /* reserved -- always 1 */
177 ((tc.tm_hour / 10) << 4) | /* Tens of hours */
178 (tc.tm_hour % 10); /* Units of hours */
179 break;
180 default:
181 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
182 }
183 return 5;
184 }
185
187 {
188 int i, j, d, of,
size;
192 frame_ptr += 6 * 80; /* skip DIF segment header */
193 for (j = 0; j < 9; j++) {
195 for (d = 8; d < 80; d+=2) {
198 continue;
199
202 }
203 frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
204 }
205 }
206 }
207
209 {
210 int j, k;
212
213 for (buf = frame; buf < frame + c->
sys->
frame_size; buf += 150 * 80) {
214 /* DV subcode: 2nd and 3d DIFs */
215 for (j = 80; j < 80 * 3; j += 80) {
216 for (k = 6; k < 6 * 8; k += 8)
218
224 }
225 }
226
227 /* DV VAUX: 4th, 5th and 6th 3DIFs */
228 for (j = 80*3 + 3; j < 80*6; j += 80) {
233 }
234 }
235 }
236
237 /*
238 * The following 3 functions constitute our interface to the world
239 */
240
243 {
244 int i, reqasize;
245
247
250 /* FIXME: we have to have more sensible approach than this one */
253
256 break;
258 for (i = 0; i < c->
n_ast && st != c->
ast[i]; i++);
259
260 /* FIXME: we have to have more sensible approach than this one */
264
266
267 /* Let us see if we've got enough audio for one DV frame. */
269
270 break;
271 default:
272 break;
273 }
274
275 /* Let us see if we have enough data to construct one DV frame. */
279 for (i=0; i < c->
n_ast; i++) {
284 }
285
287
289
291 }
292
293 return 0;
294 }
295
297 {
301 int i;
302
303 /* we support at most 1 video and 2 audio streams */
306
309
310 /* We have to sort out where audio and where video stream is */
314 if (vst)
return NULL;
316 break;
320 break;
321 default:
322 goto bail_out;
323 }
324 }
325
326 /* Some checks -- DV format is very picky about its incoming streams */
328 goto bail_out;
329 for (i=0; i<c->
n_ast; i++) {
333 goto bail_out;
337 goto bail_out;
338 }
339 }
343 goto bail_out;
344
347 goto bail_out;
349 goto bail_out;
350 }
351
353 /* only 1 stereo pair is allowed in 25Mbps mode */
354 goto bail_out;
355 }
356
357 /* Ok, everything seems to be in working order */
363
364 for (i=0; i < c->
n_ast; i++) {
366 while (i > 0) {
367 i--;
369 }
370 goto bail_out;
371 }
372 }
373
375
376 bail_out:
378 }
379
381 {
382 int i;
383 for (i=0; i < c->
n_ast; i++)
385 }
386
388 {
392
395 "Make sure that you supply exactly two streams:\n"
396 " video: 25fps or 29.97fps, audio: 2ch/48|44|32kHz/PCM\n"
397 " (50Mbps allows an optional second audio stream)\n");
398 return -1;
399 }
402 if (!tcr) { // no global timecode, look into the streams
403 int i;
406 if (tcr)
407 break;
408 }
409 }
411 return 0;
413 }
414
416 {
418 int fsize;
419
422 if (fsize > 0) {
424 }
425 return 0;
426 }
427
428 /*
429 * We might end up with some extra A/V data without matching counterpart.
430 * E.g. video data without enough audio to write the complete frame.
431 * Currently we simply drop the last frame. I don't know whether this
432 * is the best strategy of all
433 */
435 {
437 return 0;
438 }
439
443 .extensions = "dv",
450 };