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