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