Split the DV demuxer and muxer into separate files (as suggested by Diego
[libav.git] / libavformat / dvenc.c
CommitLineData
d4fc91b4
PI
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 library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28#include <time.h>
29#include "avformat.h"
30#include "dvdata.h"
31#include "dv.h"
32
33struct DVMuxContext {
34 const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */
35 int n_ast; /* Number of stereo audio streams (up to 2) */
36 AVStream *ast[2]; /* Stereo audio streams */
37 FifoBuffer audio_data[2]; /* Fifo for storing excessive amounts of PCM */
38 int frames; /* Number of a current frame */
39 time_t start_time; /* Start time of recording */
40 int has_audio; /* frame under contruction has audio */
41 int has_video; /* frame under contruction has video */
42 uint8_t frame_buf[DV_MAX_FRAME_SIZE]; /* frame under contruction */
43};
44
45static const int dv_aaux_packs_dist[12][9] = {
46 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
47 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
48 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
49 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
50 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
51 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
52 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
53 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
54 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
55 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
56 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
57 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
58};
59
60static int dv_audio_frame_size(const DVprofile* sys, int frame)
61{
62 return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist)/
63 sizeof(sys->audio_samples_dist[0]))];
64}
65
66static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf)
67{
68 struct tm tc;
69 time_t ct;
70 int ltc_frame;
71
72 /* Its hard to tell what SMPTE requires w.r.t. APT, but Quicktime needs it.
73 * We set it based on pix_fmt value but it really should be per DV profile */
74 int apt = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 1 : 0);
75
76 buf[0] = (uint8_t)pack_id;
77 switch (pack_id) {
78 case dv_timecode:
79 ct = (time_t)(c->frames / ((float)c->sys->frame_rate /
80 (float)c->sys->frame_rate_base));
81 brktimegm(ct, &tc);
82 /*
83 * LTC drop-frame frame counter drops two frames (0 and 1) every
84 * minute, unless it is exactly divisible by 10
85 */
86 ltc_frame = (c->frames + 2*ct/60 - 2*ct/600) % c->sys->ltc_divisor;
87 buf[1] = (0 << 7) | /* Color fame: 0 - unsync; 1 - sync mode */
88 (1 << 6) | /* Drop frame timecode: 0 - nondrop; 1 - drop */
89 ((ltc_frame / 10) << 4) | /* Tens of frames */
90 (ltc_frame % 10); /* Units of frames */
91 buf[2] = (1 << 7) | /* Biphase mark polarity correction: 0 - even; 1 - odd */
92 ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
93 (tc.tm_sec % 10); /* Units of seconds */
94 buf[3] = (1 << 7) | /* Binary group flag BGF0 */
95 ((tc.tm_min / 10) << 4) | /* Tens of minutes */
96 (tc.tm_min % 10); /* Units of minutes */
97 buf[4] = (1 << 7) | /* Binary group flag BGF2 */
98 (1 << 6) | /* Binary group flag BGF1 */
99 ((tc.tm_hour / 10) << 4) | /* Tens of hours */
100 (tc.tm_hour % 10); /* Units of hours */
101 break;
102 case dv_audio_source: /* AAUX source pack */
103 buf[1] = (0 << 7) | /* locked mode */
104 (1 << 6) | /* reserved -- always 1 */
105 (dv_audio_frame_size(c->sys, c->frames) -
106 c->sys->audio_min_samples[0]);
107 /* # of samples */
108 buf[2] = (0 << 7) | /* multi-stereo */
109 (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
110 (0 << 4) | /* pair bit: 0 -- one pair of channels */
111 0; /* audio mode */
112 buf[3] = (1 << 7) | /* res */
113 (1 << 6) | /* multi-language flag */
114 (c->sys->dsf << 5) | /* system: 60fields/50fields */
115 (apt << 1);/* definition: 0 -- 25Mbps, 2 -- 50Mbps */
116 buf[4] = (1 << 7) | /* emphasis: 1 -- off */
117 (0 << 6) | /* emphasis time constant: 0 -- reserved */
118 (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */
119 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
120 break;
121 case dv_audio_control:
122 buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
123 (1 << 4) | /* input source: 1 -- digital input */
124 (3 << 2) | /* compression: 3 -- no information */
125 0; /* misc. info/SMPTE emphasis off */
126 buf[2] = (1 << 7) | /* recording start point: 1 -- no */
127 (1 << 6) | /* recording end point: 1 -- no */
128 (1 << 3) | /* recording mode: 1 -- original */
129 7;
130 buf[3] = (1 << 7) | /* direction: 1 -- forward */
131 0x20; /* speed */
132 buf[4] = (1 << 7) | /* reserved -- always 1 */
133 0x7f; /* genre category */
134 break;
135 case dv_audio_recdate:
136 case dv_video_recdate: /* VAUX recording date */
137 ct = c->start_time + (time_t)(c->frames /
138 ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
139 brktimegm(ct, &tc);
140 buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
141 /* 0xff is very likely to be "unknown" */
142 buf[2] = (3 << 6) | /* reserved -- always 1 */
143 ((tc.tm_mday / 10) << 4) | /* Tens of day */
144 (tc.tm_mday % 10); /* Units of day */
145 buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
146 ((tc.tm_mon / 10) << 4) | /* Tens of month */
147 (tc.tm_mon % 10); /* Units of month */
148 buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
149 (tc.tm_year % 10); /* Units of year */
150 break;
151 case dv_audio_rectime: /* AAUX recording time */
152 case dv_video_rectime: /* VAUX recording time */
153 ct = c->start_time + (time_t)(c->frames /
154 ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
155 brktimegm(ct, &tc);
156 buf[1] = (3 << 6) | /* reserved -- always 1 */
157 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
158 buf[2] = (1 << 7) | /* reserved -- always 1 */
159 ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
160 (tc.tm_sec % 10); /* Units of seconds */
161 buf[3] = (1 << 7) | /* reserved -- always 1 */
162 ((tc.tm_min / 10) << 4) | /* Tens of minutes */
163 (tc.tm_min % 10); /* Units of minutes */
164 buf[4] = (3 << 6) | /* reserved -- always 1 */
165 ((tc.tm_hour / 10) << 4) | /* Tens of hours */
166 (tc.tm_hour % 10); /* Units of hours */
167 break;
168 default:
169 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
170 }
171 return 5;
172}
173
174static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr)
175{
176 int i, j, d, of, size;
177 size = 4 * dv_audio_frame_size(c->sys, c->frames);
178 frame_ptr += channel * c->sys->difseg_size * 150 * 80;
179 for (i = 0; i < c->sys->difseg_size; i++) {
180 frame_ptr += 6 * 80; /* skip DIF segment header */
181 for (j = 0; j < 9; j++) {
182 dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3]);
183 for (d = 8; d < 80; d+=2) {
184 of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
185 if (of*2 >= size)
186 continue;
187
188 frame_ptr[d] = fifo_peek(&c->audio_data[channel], of*2+1); // FIXME: may be we have to admit
189 frame_ptr[d+1] = fifo_peek(&c->audio_data[channel], of*2); // that DV is a big endian PCM
190 }
191 frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
192 }
193 }
194}
195
196static void dv_inject_metadata(DVMuxContext *c, uint8_t* frame)
197{
198 int j, k;
199 uint8_t* buf;
200
201 for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80) {
202 /* DV subcode: 2nd and 3d DIFs */
203 for (j = 80; j < 80 * 3; j += 80) {
204 for (k = 6; k < 6 * 8; k += 8)
205 dv_write_pack(dv_timecode, c, &buf[j+k]);
206
207 if (((long)(buf-frame)/(c->sys->frame_size/(c->sys->difseg_size*c->sys->n_difchan))%c->sys->difseg_size) > 5) { /* FIXME: is this really needed ? */
208 dv_write_pack(dv_video_recdate, c, &buf[j+14]);
209 dv_write_pack(dv_video_rectime, c, &buf[j+22]);
210 dv_write_pack(dv_video_recdate, c, &buf[j+38]);
211 dv_write_pack(dv_video_rectime, c, &buf[j+46]);
212 }
213 }
214
215 /* DV VAUX: 4th, 5th and 6th 3DIFs */
216 for (j = 80*3 + 3; j < 80*6; j += 80) {
217 dv_write_pack(dv_video_recdate, c, &buf[j+5*2]);
218 dv_write_pack(dv_video_rectime, c, &buf[j+5*3]);
219 dv_write_pack(dv_video_recdate, c, &buf[j+5*11]);
220 dv_write_pack(dv_video_rectime, c, &buf[j+5*12]);
221 }
222 }
223}
224
225/*
226 * The following 3 functions constitute our interface to the world
227 */
228
229int dv_assemble_frame(DVMuxContext *c, AVStream* st,
230 const uint8_t* data, int data_size, uint8_t** frame)
231{
232 int i, reqasize;
233
234 *frame = &c->frame_buf[0];
235 reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
236
237 switch (st->codec->codec_type) {
238 case CODEC_TYPE_VIDEO:
239 /* FIXME: we have to have more sensible approach than this one */
240 if (c->has_video)
241 av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
242
243 memcpy(*frame, data, c->sys->frame_size);
244 c->has_video = 1;
245 break;
246 case CODEC_TYPE_AUDIO:
247 for (i = 0; i < c->n_ast && st != c->ast[i]; i++);
248
249 /* FIXME: we have to have more sensible approach than this one */
250 if (fifo_size(&c->audio_data[i], c->audio_data[i].rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
251 av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
252 fifo_write(&c->audio_data[i], data, data_size, &c->audio_data[i].wptr);
253
254 /* Lets see if we've got enough audio for one DV frame */
255 c->has_audio |= ((reqasize <= fifo_size(&c->audio_data[i], c->audio_data[i].rptr)) << i);
256
257 break;
258 default:
259 break;
260 }
261
262 /* Lets see if we have enough data to construct one DV frame */
263 if (c->has_video == 1 && c->has_audio + 1 == 1<<c->n_ast) {
264 dv_inject_metadata(c, *frame);
265 for (i=0; i<c->n_ast; i++) {
266 dv_inject_audio(c, i, *frame);
267 fifo_drain(&c->audio_data[i], reqasize);
268 }
269
270 c->has_video = 0;
271 c->has_audio = 0;
272 c->frames++;
273
274 return c->sys->frame_size;
275 }
276
277 return 0;
278}
279
280DVMuxContext* dv_init_mux(AVFormatContext* s)
281{
282 DVMuxContext *c;
283 AVStream *vst = NULL;
284 int i;
285
286 /* we support at most 1 video and 2 audio streams */
287 if (s->nb_streams > 3)
288 return NULL;
289
290 c = av_mallocz(sizeof(DVMuxContext));
291 if (!c)
292 return NULL;
293
294 c->n_ast = 0;
295 c->ast[0] = c->ast[1] = NULL;
296
297 /* We have to sort out where audio and where video stream is */
298 for (i=0; i<s->nb_streams; i++) {
299 switch (s->streams[i]->codec->codec_type) {
300 case CODEC_TYPE_VIDEO:
301 vst = s->streams[i];
302 break;
303 case CODEC_TYPE_AUDIO:
304 c->ast[c->n_ast++] = s->streams[i];
305 break;
306 default:
307 goto bail_out;
308 }
309 }
310
311 /* Some checks -- DV format is very picky about its incoming streams */
312 if (!vst || vst->codec->codec_id != CODEC_ID_DVVIDEO)
313 goto bail_out;
314 for (i=0; i<c->n_ast; i++) {
315 if (c->ast[i] && (c->ast[i]->codec->codec_id != CODEC_ID_PCM_S16LE ||
316 c->ast[i]->codec->sample_rate != 48000 ||
317 c->ast[i]->codec->channels != 2))
318 goto bail_out;
319 }
320 c->sys = dv_codec_profile(vst->codec);
321 if (!c->sys)
322 goto bail_out;
323
324 if((c->n_ast > 1) && (c->sys->n_difchan < 2)) {
325 /* only 1 stereo pair is allowed in 25Mbps mode */
326 goto bail_out;
327 }
328
329 /* Ok, everything seems to be in working order */
330 c->frames = 0;
331 c->has_audio = 0;
332 c->has_video = 0;
333 c->start_time = (time_t)s->timestamp;
334
335 for (i=0; i<c->n_ast; i++) {
336 if (c->ast[i] && fifo_init(&c->audio_data[i], 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) {
337 while (i>0) {
338 i--;
339 fifo_free(&c->audio_data[i]);
340 }
341 goto bail_out;
342 }
343 }
344
345 return c;
346
347bail_out:
348 av_free(c);
349 return NULL;
350}
351
352void dv_delete_mux(DVMuxContext *c)
353{
354 int i;
355 for (i=0; i < c->n_ast; i++)
356 fifo_free(&c->audio_data[i]);
357}
358
359#ifdef CONFIG_MUXERS
360static int dv_write_header(AVFormatContext *s)
361{
362 s->priv_data = dv_init_mux(s);
363 if (!s->priv_data) {
364 av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
365 "Make sure that you supply exactly two streams:\n"
366 " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n"
367 " (50Mbps allows an optional second audio stream)\n");
368 return -1;
369 }
370 return 0;
371}
372
373static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt)
374{
375 uint8_t* frame;
376 int fsize;
377
378 fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index],
379 pkt->data, pkt->size, &frame);
380 if (fsize > 0) {
381 put_buffer(&s->pb, frame, fsize);
382 put_flush_packet(&s->pb);
383 }
384 return 0;
385}
386
387/*
388 * We might end up with some extra A/V data without matching counterpart.
389 * E.g. video data without enough audio to write the complete frame.
390 * Currently we simply drop the last frame. I don't know whether this
391 * is the best strategy of all
392 */
393static int dv_write_trailer(struct AVFormatContext *s)
394{
395 dv_delete_mux((DVMuxContext *)s->priv_data);
396 return 0;
397}
398#endif /* CONFIG_MUXERS */
399
400#ifdef CONFIG_DV_MUXER
401AVOutputFormat dv_muxer = {
402 "dv",
403 "DV video format",
404 NULL,
405 "dv",
406 sizeof(DVMuxContext),
407 CODEC_ID_PCM_S16LE,
408 CODEC_ID_DVVIDEO,
409 dv_write_header,
410 dv_write_packet,
411 dv_write_trailer,
412};
413#endif