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