Libav 0.7.1
|
00001 /* 00002 * General DV muxer/demuxer 00003 * Copyright (c) 2003 Roman Shaposhnik 00004 * 00005 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth 00006 * of DV technical info. 00007 * 00008 * Raw DV format 00009 * Copyright (c) 2002 Fabrice Bellard 00010 * 00011 * 50 Mbps (DVCPRO50) support 00012 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com> 00013 * 00014 * This file is part of Libav. 00015 * 00016 * Libav is free software; you can redistribute it and/or 00017 * modify it under the terms of the GNU Lesser General Public 00018 * License as published by the Free Software Foundation; either 00019 * version 2.1 of the License, or (at your option) any later version. 00020 * 00021 * Libav is distributed in the hope that it will be useful, 00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00024 * Lesser General Public License for more details. 00025 * 00026 * You should have received a copy of the GNU Lesser General Public 00027 * License along with Libav; if not, write to the Free Software 00028 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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; /* current DV profile, e.g.: 525/60, 625/50 */ 00041 int n_ast; /* number of stereo audio streams (up to 2) */ 00042 AVStream *ast[2]; /* stereo audio streams */ 00043 AVFifoBuffer *audio_data[2]; /* FIFO for storing excessive amounts of PCM */ 00044 int frames; /* current frame number */ 00045 time_t start_time; /* recording start time */ 00046 int has_audio; /* frame under contruction has audio */ 00047 int has_video; /* frame under contruction has video */ 00048 uint8_t frame_buf[DV_MAX_FRAME_SIZE]; /* frame under contruction */ 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 * LTC drop-frame frame counter drops two frames (0 and 1) every 00087 * minute, unless it is exactly divisible by 10 00088 */ 00089 ltc_frame = (c->frames + 2 * ct / 60 - 2 * ct / 600) % c->sys->ltc_divisor; 00090 buf[1] = (0 << 7) | /* color frame: 0 - unsync; 1 - sync mode */ 00091 (1 << 6) | /* drop frame timecode: 0 - nondrop; 1 - drop */ 00092 ((ltc_frame / 10) << 4) | /* tens of frames */ 00093 (ltc_frame % 10); /* units of frames */ 00094 buf[2] = (1 << 7) | /* biphase mark polarity correction: 0 - even; 1 - odd */ 00095 ((tc.tm_sec / 10) << 4) | /* tens of seconds */ 00096 (tc.tm_sec % 10); /* units of seconds */ 00097 buf[3] = (1 << 7) | /* binary group flag BGF0 */ 00098 ((tc.tm_min / 10) << 4) | /* tens of minutes */ 00099 (tc.tm_min % 10); /* units of minutes */ 00100 buf[4] = (1 << 7) | /* binary group flag BGF2 */ 00101 (1 << 6) | /* binary group flag BGF1 */ 00102 ((tc.tm_hour / 10) << 4) | /* tens of hours */ 00103 (tc.tm_hour % 10); /* units of hours */ 00104 break; 00105 case dv_audio_source: /* AAUX source pack */ 00106 va_start(ap, buf); 00107 buf[1] = (1 << 7) | /* locked mode -- SMPTE only supports locked mode */ 00108 (1 << 6) | /* reserved -- always 1 */ 00109 (dv_audio_frame_size(c->sys, c->frames) - 00110 c->sys->audio_min_samples[0]); 00111 /* # of samples */ 00112 buf[2] = (0 << 7) | /* multi-stereo */ 00113 (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */ 00114 (0 << 4) | /* pair bit: 0 -- one pair of channels */ 00115 !!va_arg(ap, int); /* audio mode */ 00116 buf[3] = (1 << 7) | /* res */ 00117 (1 << 6) | /* multi-language flag */ 00118 (c->sys->dsf << 5) | /* system: 60fields/50fields */ 00119 (c->sys->n_difchan & 2); /* definition: 0 -- 25Mbps, 2 -- 50Mbps */ 00120 buf[4] = (1 << 7) | /* emphasis: 1 -- off */ 00121 (0 << 6) | /* emphasis time constant: 0 -- reserved */ 00122 (0 << 3) | /* frequency: 0 -- 48kHz, 1 -- 44,1kHz, 2 -- 32kHz */ 00123 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */ 00124 va_end(ap); 00125 break; 00126 case dv_audio_control: 00127 buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */ 00128 (1 << 4) | /* input source: 1 -- digital input */ 00129 (3 << 2) | /* compression: 3 -- no information */ 00130 0; /* misc. info/SMPTE emphasis off */ 00131 buf[2] = (1 << 7) | /* recording start point: 1 -- no */ 00132 (1 << 6) | /* recording end point: 1 -- no */ 00133 (1 << 3) | /* recording mode: 1 -- original */ 00134 7; 00135 buf[3] = (1 << 7) | /* direction: 1 -- forward */ 00136 (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0x20 : /* speed */ 00137 c->sys->ltc_divisor * 4); 00138 buf[4] = (1 << 7) | /* reserved -- always 1 */ 00139 0x7f; /* genre category */ 00140 break; 00141 case dv_audio_recdate: 00142 case dv_video_recdate: /* VAUX recording date */ 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; /* ds, tm, tens of time zone, units of time zone */ 00147 /* 0xff is very likely to be "unknown" */ 00148 buf[2] = (3 << 6) | /* reserved -- always 1 */ 00149 ((tc.tm_mday / 10) << 4) | /* Tens of day */ 00150 (tc.tm_mday % 10); /* Units of day */ 00151 buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */ 00152 ((tc.tm_mon / 10) << 4) | /* Tens of month */ 00153 (tc.tm_mon % 10); /* Units of month */ 00154 buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */ 00155 (tc.tm_year % 10); /* Units of year */ 00156 break; 00157 case dv_audio_rectime: /* AAUX recording time */ 00158 case dv_video_rectime: /* VAUX recording time */ 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) | /* reserved -- always 1 */ 00163 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */ 00164 buf[2] = (1 << 7) | /* reserved -- always 1 */ 00165 ((tc.tm_sec / 10) << 4) | /* Tens of seconds */ 00166 (tc.tm_sec % 10); /* Units of seconds */ 00167 buf[3] = (1 << 7) | /* reserved -- always 1 */ 00168 ((tc.tm_min / 10) << 4) | /* Tens of minutes */ 00169 (tc.tm_min % 10); /* Units of minutes */ 00170 buf[4] = (3 << 6) | /* reserved -- always 1 */ 00171 ((tc.tm_hour / 10) << 4) | /* Tens of hours */ 00172 (tc.tm_hour % 10); /* Units of hours */ 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; /* skip DIF segment header */ 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); // FIXME: maybe we have to admit 00195 frame_ptr[d+1] = av_fifo_peek(c->audio_data[channel], of*2); // that DV is a big-endian PCM 00196 } 00197 frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ 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 /* DV subcode: 2nd and 3d DIFs */ 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) { /* FIXME: is this really needed ? */ 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 /* DV VAUX: 4th, 5th and 6th 3DIFs */ 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 * The following 3 functions constitute our interface to the world 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 /* FIXME: we have to have more sensible approach than this one */ 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 /* FIXME: we have to have more sensible approach than this one */ 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 /* Let us see if we've got enough audio for one DV frame. */ 00261 c->has_audio |= ((reqasize <= av_fifo_size(c->audio_data[i])) << i); 00262 00263 break; 00264 default: 00265 break; 00266 } 00267 00268 /* Let us see if we have enough data to construct one DV frame. */ 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 /* we support at most 1 video and 2 audio streams */ 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 /* We have to sort out where audio and where video stream is */ 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 /* Some checks -- DV format is very picky about its incoming streams */ 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 /* only 1 stereo pair is allowed in 25Mbps mode */ 00332 goto bail_out; 00333 } 00334 00335 /* Ok, everything seems to be in working order */ 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 * We might end up with some extra A/V data without matching counterpart. 00392 * E.g. video data without enough audio to write the complete frame. 00393 * Currently we simply drop the last frame. I don't know whether this 00394 * is the best strategy of all 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 };