00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <time.h>
00031 #include <stdarg.h>
00032
00033 #include "avformat.h"
00034 #include "internal.h"
00035 #include "libavcodec/dvdata.h"
00036 #include "dv.h"
00037 #include "libavutil/fifo.h"
00038
00039 struct DVMuxContext {
00040 const DVprofile* sys;
00041 int n_ast;
00042 AVStream *ast[2];
00043 AVFifoBuffer *audio_data[2];
00044 int frames;
00045 time_t start_time;
00046 int has_audio;
00047 int has_video;
00048 uint8_t frame_buf[DV_MAX_FRAME_SIZE];
00049 };
00050
00051 static const int dv_aaux_packs_dist[12][9] = {
00052 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
00053 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
00054 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
00055 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
00056 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
00057 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
00058 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
00059 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
00060 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
00061 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
00062 { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
00063 { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
00064 };
00065
00066 static int dv_audio_frame_size(const DVprofile* sys, int frame)
00067 {
00068 return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist) /
00069 sizeof(sys->audio_samples_dist[0]))];
00070 }
00071
00072 static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf, ...)
00073 {
00074 struct tm tc;
00075 time_t ct;
00076 int ltc_frame;
00077 va_list ap;
00078
00079 buf[0] = (uint8_t)pack_id;
00080 switch (pack_id) {
00081 case dv_timecode:
00082 ct = (time_t)av_rescale_rnd(c->frames, c->sys->time_base.num,
00083 c->sys->time_base.den, AV_ROUND_DOWN);
00084 brktimegm(ct, &tc);
00085
00086
00087
00088
00089 ltc_frame = (c->frames + 2 * ct / 60 - 2 * ct / 600) % c->sys->ltc_divisor;
00090 buf[1] = (0 << 7) |
00091 (1 << 6) |
00092 ((ltc_frame / 10) << 4) |
00093 (ltc_frame % 10);
00094 buf[2] = (1 << 7) |
00095 ((tc.tm_sec / 10) << 4) |
00096 (tc.tm_sec % 10);
00097 buf[3] = (1 << 7) |
00098 ((tc.tm_min / 10) << 4) |
00099 (tc.tm_min % 10);
00100 buf[4] = (1 << 7) |
00101 (1 << 6) |
00102 ((tc.tm_hour / 10) << 4) |
00103 (tc.tm_hour % 10);
00104 break;
00105 case dv_audio_source:
00106 va_start(ap, buf);
00107 buf[1] = (1 << 7) |
00108 (1 << 6) |
00109 (dv_audio_frame_size(c->sys, c->frames) -
00110 c->sys->audio_min_samples[0]);
00111
00112 buf[2] = (0 << 7) |
00113 (0 << 5) |
00114 (0 << 4) |
00115 !!va_arg(ap, int);
00116 buf[3] = (1 << 7) |
00117 (1 << 6) |
00118 (c->sys->dsf << 5) |
00119 (c->sys->n_difchan & 2);
00120 buf[4] = (1 << 7) |
00121 (0 << 6) |
00122 (0 << 3) |
00123 0;
00124 va_end(ap);
00125 break;
00126 case dv_audio_control:
00127 buf[1] = (0 << 6) |
00128 (1 << 4) |
00129 (3 << 2) |
00130 0;
00131 buf[2] = (1 << 7) |
00132 (1 << 6) |
00133 (1 << 3) |
00134 7;
00135 buf[3] = (1 << 7) |
00136 (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0x20 :
00137 c->sys->ltc_divisor * 4);
00138 buf[4] = (1 << 7) |
00139 0x7f;
00140 break;
00141 case dv_audio_recdate:
00142 case dv_video_recdate:
00143 ct = c->start_time + av_rescale_rnd(c->frames, c->sys->time_base.num,
00144 c->sys->time_base.den, AV_ROUND_DOWN);
00145 brktimegm(ct, &tc);
00146 buf[1] = 0xff;
00147
00148 buf[2] = (3 << 6) |
00149 ((tc.tm_mday / 10) << 4) |
00150 (tc.tm_mday % 10);
00151 buf[3] =
00152 ((tc.tm_mon / 10) << 4) |
00153 (tc.tm_mon % 10);
00154 buf[4] = (((tc.tm_year % 100) / 10) << 4) |
00155 (tc.tm_year % 10);
00156 break;
00157 case dv_audio_rectime:
00158 case dv_video_rectime:
00159 ct = c->start_time + av_rescale_rnd(c->frames, c->sys->time_base.num,
00160 c->sys->time_base.den, AV_ROUND_DOWN);
00161 brktimegm(ct, &tc);
00162 buf[1] = (3 << 6) |
00163 0x3f;
00164 buf[2] = (1 << 7) |
00165 ((tc.tm_sec / 10) << 4) |
00166 (tc.tm_sec % 10);
00167 buf[3] = (1 << 7) |
00168 ((tc.tm_min / 10) << 4) |
00169 (tc.tm_min % 10);
00170 buf[4] = (3 << 6) |
00171 ((tc.tm_hour / 10) << 4) |
00172 (tc.tm_hour % 10);
00173 break;
00174 default:
00175 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
00176 }
00177 return 5;
00178 }
00179
00180 static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr)
00181 {
00182 int i, j, d, of, size;
00183 size = 4 * dv_audio_frame_size(c->sys, c->frames);
00184 frame_ptr += channel * c->sys->difseg_size * 150 * 80;
00185 for (i = 0; i < c->sys->difseg_size; i++) {
00186 frame_ptr += 6 * 80;
00187 for (j = 0; j < 9; j++) {
00188 dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3], i >= c->sys->difseg_size/2);
00189 for (d = 8; d < 80; d+=2) {
00190 of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
00191 if (of*2 >= size)
00192 continue;
00193
00194 frame_ptr[d] = av_fifo_peek(c->audio_data[channel], of*2+1);
00195 frame_ptr[d+1] = av_fifo_peek(c->audio_data[channel], of*2);
00196 }
00197 frame_ptr += 16 * 80;
00198 }
00199 }
00200 }
00201
00202 static void dv_inject_metadata(DVMuxContext *c, uint8_t* frame)
00203 {
00204 int j, k;
00205 uint8_t* buf;
00206
00207 for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80) {
00208
00209 for (j = 80; j < 80 * 3; j += 80) {
00210 for (k = 6; k < 6 * 8; k += 8)
00211 dv_write_pack(dv_timecode, c, &buf[j+k]);
00212
00213 if (((long)(buf-frame)/(c->sys->frame_size/(c->sys->difseg_size*c->sys->n_difchan))%c->sys->difseg_size) > 5) {
00214 dv_write_pack(dv_video_recdate, c, &buf[j+14]);
00215 dv_write_pack(dv_video_rectime, c, &buf[j+22]);
00216 dv_write_pack(dv_video_recdate, c, &buf[j+38]);
00217 dv_write_pack(dv_video_rectime, c, &buf[j+46]);
00218 }
00219 }
00220
00221
00222 for (j = 80*3 + 3; j < 80*6; j += 80) {
00223 dv_write_pack(dv_video_recdate, c, &buf[j+5*2]);
00224 dv_write_pack(dv_video_rectime, c, &buf[j+5*3]);
00225 dv_write_pack(dv_video_recdate, c, &buf[j+5*11]);
00226 dv_write_pack(dv_video_rectime, c, &buf[j+5*12]);
00227 }
00228 }
00229 }
00230
00231
00232
00233
00234
00235 static int dv_assemble_frame(DVMuxContext *c, AVStream* st,
00236 uint8_t* data, int data_size, uint8_t** frame)
00237 {
00238 int i, reqasize;
00239
00240 *frame = &c->frame_buf[0];
00241 reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
00242
00243 switch (st->codec->codec_type) {
00244 case AVMEDIA_TYPE_VIDEO:
00245
00246 if (c->has_video)
00247 av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
00248
00249 memcpy(*frame, data, c->sys->frame_size);
00250 c->has_video = 1;
00251 break;
00252 case AVMEDIA_TYPE_AUDIO:
00253 for (i = 0; i < c->n_ast && st != c->ast[i]; i++);
00254
00255
00256 if (av_fifo_size(c->audio_data[i]) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
00257 av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
00258 av_fifo_generic_write(c->audio_data[i], data, data_size, NULL);
00259
00260
00261 c->has_audio |= ((reqasize <= av_fifo_size(c->audio_data[i])) << i);
00262
00263 break;
00264 default:
00265 break;
00266 }
00267
00268
00269 if (c->has_video == 1 && c->has_audio + 1 == 1 << c->n_ast) {
00270 dv_inject_metadata(c, *frame);
00271 c->has_audio = 0;
00272 for (i=0; i < c->n_ast; i++) {
00273 dv_inject_audio(c, i, *frame);
00274 av_fifo_drain(c->audio_data[i], reqasize);
00275 c->has_audio |= ((reqasize <= av_fifo_size(c->audio_data[i])) << i);
00276 }
00277
00278 c->has_video = 0;
00279
00280 c->frames++;
00281
00282 return c->sys->frame_size;
00283 }
00284
00285 return 0;
00286 }
00287
00288 static DVMuxContext* dv_init_mux(AVFormatContext* s)
00289 {
00290 DVMuxContext *c = s->priv_data;
00291 AVStream *vst = NULL;
00292 int i;
00293
00294
00295 if (s->nb_streams > 3)
00296 return NULL;
00297
00298 c->n_ast = 0;
00299 c->ast[0] = c->ast[1] = NULL;
00300
00301
00302 for (i=0; i<s->nb_streams; i++) {
00303 switch (s->streams[i]->codec->codec_type) {
00304 case AVMEDIA_TYPE_VIDEO:
00305 if (vst) return NULL;
00306 vst = s->streams[i];
00307 break;
00308 case AVMEDIA_TYPE_AUDIO:
00309 if (c->n_ast > 1) return NULL;
00310 c->ast[c->n_ast++] = s->streams[i];
00311 break;
00312 default:
00313 goto bail_out;
00314 }
00315 }
00316
00317
00318 if (!vst || vst->codec->codec_id != CODEC_ID_DVVIDEO)
00319 goto bail_out;
00320 for (i=0; i<c->n_ast; i++) {
00321 if (c->ast[i] && (c->ast[i]->codec->codec_id != CODEC_ID_PCM_S16LE ||
00322 c->ast[i]->codec->sample_rate != 48000 ||
00323 c->ast[i]->codec->channels != 2))
00324 goto bail_out;
00325 }
00326 c->sys = ff_dv_codec_profile(vst->codec);
00327 if (!c->sys)
00328 goto bail_out;
00329
00330 if ((c->n_ast > 1) && (c->sys->n_difchan < 2)) {
00331
00332 goto bail_out;
00333 }
00334
00335
00336 c->frames = 0;
00337 c->has_audio = 0;
00338 c->has_video = 0;
00339 c->start_time = (time_t)s->timestamp;
00340
00341 for (i=0; i < c->n_ast; i++) {
00342 if (c->ast[i] && !(c->audio_data[i]=av_fifo_alloc(100*AVCODEC_MAX_AUDIO_FRAME_SIZE))) {
00343 while (i > 0) {
00344 i--;
00345 av_fifo_free(c->audio_data[i]);
00346 }
00347 goto bail_out;
00348 }
00349 }
00350
00351 return c;
00352
00353 bail_out:
00354 return NULL;
00355 }
00356
00357 static void dv_delete_mux(DVMuxContext *c)
00358 {
00359 int i;
00360 for (i=0; i < c->n_ast; i++)
00361 av_fifo_free(c->audio_data[i]);
00362 }
00363
00364 static int dv_write_header(AVFormatContext *s)
00365 {
00366 if (!dv_init_mux(s)) {
00367 av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
00368 "Make sure that you supply exactly two streams:\n"
00369 " video: 25fps or 29.97fps, audio: 2ch/48kHz/PCM\n"
00370 " (50Mbps allows an optional second audio stream)\n");
00371 return -1;
00372 }
00373 return 0;
00374 }
00375
00376 static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt)
00377 {
00378 uint8_t* frame;
00379 int fsize;
00380
00381 fsize = dv_assemble_frame(s->priv_data, s->streams[pkt->stream_index],
00382 pkt->data, pkt->size, &frame);
00383 if (fsize > 0) {
00384 avio_write(s->pb, frame, fsize);
00385 avio_flush(s->pb);
00386 }
00387 return 0;
00388 }
00389
00390
00391
00392
00393
00394
00395
00396 static int dv_write_trailer(struct AVFormatContext *s)
00397 {
00398 dv_delete_mux(s->priv_data);
00399 return 0;
00400 }
00401
00402 AVOutputFormat ff_dv_muxer = {
00403 "dv",
00404 NULL_IF_CONFIG_SMALL("DV video format"),
00405 NULL,
00406 "dv",
00407 sizeof(DVMuxContext),
00408 CODEC_ID_PCM_S16LE,
00409 CODEC_ID_DVVIDEO,
00410 dv_write_header,
00411 dv_write_packet,
00412 dv_write_trailer,
00413 };