• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/dvenc.c

Go to the documentation of this file.
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 #include "libavutil/mathematics.h"
00039 
00040 struct DVMuxContext {
00041     const DVprofile*  sys;           /* current DV profile, e.g.: 525/60, 625/50 */
00042     int               n_ast;         /* number of stereo audio streams (up to 2) */
00043     AVStream         *ast[2];        /* stereo audio streams */
00044     AVFifoBuffer     *audio_data[2]; /* FIFO for storing excessive amounts of PCM */
00045     int               frames;        /* current frame number */
00046     int64_t           start_time;    /* recording start time */
00047     int               has_audio;     /* frame under contruction has audio */
00048     int               has_video;     /* frame under contruction has video */
00049     uint8_t           frame_buf[DV_MAX_FRAME_SIZE]; /* frame under contruction */
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          * LTC drop-frame frame counter drops two frames (0 and 1) every
00088          * minute, unless it is exactly divisible by 10
00089          */
00090         ltc_frame = (c->frames + 2 * ct / 60 - 2 * ct / 600) % c->sys->ltc_divisor;
00091         buf[1] = (0                 << 7) | /* color frame: 0 - unsync; 1 - sync mode */
00092                  (1                 << 6) | /* drop frame timecode: 0 - nondrop; 1 - drop */
00093                  ((ltc_frame / 10)  << 4) | /* tens of frames */
00094                  (ltc_frame % 10);          /* units of frames */
00095         buf[2] = (1                 << 7) | /* biphase mark polarity correction: 0 - even; 1 - odd */
00096                  ((tc.tm_sec / 10)  << 4) | /* tens of seconds */
00097                  (tc.tm_sec % 10);          /* units of seconds */
00098         buf[3] = (1                 << 7) | /* binary group flag BGF0 */
00099                  ((tc.tm_min / 10)  << 4) | /* tens of minutes */
00100                  (tc.tm_min % 10);          /* units of minutes */
00101         buf[4] = (1                 << 7) | /* binary group flag BGF2 */
00102                  (1                 << 6) | /* binary group flag BGF1 */
00103                  ((tc.tm_hour / 10) << 4) | /* tens of hours */
00104                  (tc.tm_hour % 10);         /* units of hours */
00105         break;
00106     case dv_audio_source:  /* AAUX source pack */
00107         va_start(ap, buf);
00108         buf[1] = (1 << 7) | /* locked mode -- SMPTE only supports locked mode */
00109                  (1 << 6) | /* reserved -- always 1 */
00110                  (dv_audio_frame_size(c->sys, c->frames) -
00111                   c->sys->audio_min_samples[0]);
00112                             /* # of samples      */
00113         buf[2] = (0 << 7) | /* multi-stereo      */
00114                  (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
00115                  (0 << 4) | /* pair bit: 0 -- one pair of channels */
00116                  !!va_arg(ap, int); /* audio mode        */
00117         buf[3] = (1 << 7) | /* res               */
00118                  (1 << 6) | /* multi-language flag */
00119                  (c->sys->dsf << 5) | /*  system: 60fields/50fields */
00120                  (c->sys->n_difchan & 2); /* definition: 0 -- 25Mbps, 2 -- 50Mbps */
00121         buf[4] = (1 << 7) | /* emphasis: 1 -- off */
00122                  (0 << 6) | /* emphasis time constant: 0 -- reserved */
00123                  (0 << 3) | /* frequency: 0 -- 48kHz, 1 -- 44,1kHz, 2 -- 32kHz */
00124                   0;        /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
00125         va_end(ap);
00126         break;
00127     case dv_audio_control:
00128         buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
00129                  (1 << 4) | /* input source: 1 -- digital input */
00130                  (3 << 2) | /* compression: 3 -- no information */
00131                   0;        /* misc. info/SMPTE emphasis off */
00132         buf[2] = (1 << 7) | /* recording start point: 1 -- no */
00133                  (1 << 6) | /* recording end point: 1 -- no */
00134                  (1 << 3) | /* recording mode: 1 -- original */
00135                   7;
00136         buf[3] = (1 << 7) | /* direction: 1 -- forward */
00137                  (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0x20 : /* speed */
00138                                                        c->sys->ltc_divisor * 4);
00139         buf[4] = (1 << 7) | /* reserved -- always 1 */
00140                   0x7f;     /* genre category */
00141         break;
00142     case dv_audio_recdate:
00143     case dv_video_recdate:  /* VAUX recording date */
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; /* ds, tm, tens of time zone, units of time zone */
00148                        /* 0xff is very likely to be "unknown" */
00149         buf[2] = (3 << 6) | /* reserved -- always 1 */
00150                  ((tc.tm_mday / 10) << 4) | /* Tens of day */
00151                  (tc.tm_mday % 10);         /* Units of day */
00152         buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
00153                  ((tc.tm_mon / 10) << 4) |    /* Tens of month */
00154                  (tc.tm_mon  % 10);           /* Units of month */
00155         buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
00156                  (tc.tm_year % 10);                 /* Units of year */
00157         break;
00158     case dv_audio_rectime:  /* AAUX recording time */
00159     case dv_video_rectime:  /* VAUX recording time */
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) | /* reserved -- always 1 */
00164                  0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
00165         buf[2] = (1 << 7) | /* reserved -- always 1 */
00166                  ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
00167                  (tc.tm_sec % 10);         /* Units of seconds */
00168         buf[3] = (1 << 7) | /* reserved -- always 1 */
00169                  ((tc.tm_min / 10) << 4) | /* Tens of minutes */
00170                  (tc.tm_min % 10);         /* Units of minutes */
00171         buf[4] = (3 << 6) | /* reserved -- always 1 */
00172                  ((tc.tm_hour / 10) << 4) | /* Tens of hours */
00173                  (tc.tm_hour % 10);         /* Units of hours */
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; /* skip DIF segment header */
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); // FIXME: maybe we have to admit
00196                 frame_ptr[d+1] = *av_fifo_peek2(c->audio_data[channel], of*2);   //        that DV is a big-endian PCM
00197             }
00198             frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
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         /* DV subcode: 2nd and 3d DIFs */
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) { /* FIXME: is this really needed ? */
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         /* DV VAUX: 4th, 5th and 6th 3DIFs */
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  * The following 3 functions constitute our interface to the world
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         /* FIXME: we have to have more sensible approach than this one */
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           /* FIXME: we have to have more sensible approach than this one */
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         /* Let us see if we've got enough audio for one DV frame. */
00262         c->has_audio |= ((reqasize <= av_fifo_size(c->audio_data[i])) << i);
00263 
00264         break;
00265     default:
00266         break;
00267     }
00268 
00269     /* Let us see if we have enough data to construct one DV frame. */
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     /* we support at most 1 video and 2 audio streams */
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     /* We have to sort out where audio and where video stream is */
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     /* Some checks -- DV format is very picky about its incoming streams */
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         /* only 1 stereo pair is allowed in 25Mbps mode */
00334         goto bail_out;
00335     }
00336 
00337     /* Ok, everything seems to be in working order */
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  * We might end up with some extra A/V data without matching counterpart.
00400  * E.g. video data without enough audio to write the complete frame.
00401  * Currently we simply drop the last frame. I don't know whether this
00402  * is the best strategy of all
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 };
Generated on Sat Mar 17 2012 12:57:53 for Libav by doxygen 1.7.1