[Fwd: Re: [Ffmpeg-devel] DV Metadata Patch]

Roman Shaposhnik rvs
Fri Aug 25 05:58:19 CEST 2006


It looks like it still needs a bit of cleaning (i.e. there's no need to
iterate over all the PACKs we know about if we are only setting 
some of them, tables still need folding, etc.) but I think it'll
be easier for me to do that, especially now that I have a bit 
more time available.

Anyway -- I'll apply it in one form or another in a couple of days. 

Thanks,
Roman.


On Tue, 2006-08-22 at 23:44 -0500, Brian Brice wrote:
> Brian Brice wrote:
> > Roman Shaposhnik wrote:
> >> On Wed, Jul 26, 2006 at 08:04:51PM -0500, Brian Brice wrote:
> >>> > I've been working on getting metadata for DV encoded frames written
> >>> > properly within the frame's header.  It seems as if lavf/dv.c's
> >>> > dv_format_frame() is the function that can perform this task, except
> >>> > it should be done within the codec level rather than output format.
> >>
> >>   Nice try ;-)
> >>
> >>> > The patch I've attached is pretty much moving those functions from the
> >>> > muxer side to the codec.  I'm a bit stuck on the organization that you
> >>> > guys follow though.  This code calls lavf/cutils.c's brktimegm().  I
> >>> > copied this function from lavf to lavc just as a temporary "get it to
> >>> > work" solution.  The functions in lavf/cutils.c, couldn't they go into
> >>> > lavu?
> >>
> >>   That's the trouble -- some of these functions are truly muxer related
> >> and *can not* be moved to the codec level successfully. Everything that
> >> deals with date/time/#frames falls into that category.
> >>> > Anyway, I'm sending this patch to you guys for advice on how to 
> >>> organize
> >>> > this "move" more efficiently within the library.  Thanks!
> >>
> >>   I'll be the one to approve it. So far you're on the right track,
> >> however:
> >>     1. It seems that the BIG UGLY TABLES (tm) are no longer needed.
> >>      I put them there for testing purposes -- but haven't had
> >>      a single complaint yet. Please consider folding them      into 
> >> nice if-then-else code.
> >>
> >>   2. The following PACKs should remain in libavformat/dv.c or at least
> >>      not be fully set:
> >>            dv_timecode             dv_audio_source
> >>            dv_audio_control
> >>            dv_audio_recdate
> >>            dv_audio_rectime
> >>
> >>   3. The following PACKs *theoretically* can be set in 
> >> libavcodec/dv.c, but      you might find it problematic to convince 
> >> security minded folks to do that:
> >>           dv_video_recdate
> >>           dv_video_rectime
> >>   
> > 
> > Sorry for taking a while.  Here's a patch that will write preliminary
> > metadata within DV frames on the codec level.  This is useful for when
> > DV is encapsulate in AVI or QuickTime files.  It writes the following
> > sections when encoding a video frame:
> >     dv_header525
> >     dv_header625
> >     dv_video_source
> >     dv_video_control
> > 
> > The remaining sections are left to the DV DIF output format to fill in.
> > Some functionality was taken out of lavf/dv.c because it'll be done in
> > the codec instead.  It also makes sure not to overwrite what the codec
> > has set.
> > 
> > Some types were moved to lavc/dvdata.h because they're shared by both
> > lavc and lavf.
> > 
> > I'm actually not sure what those BIG UGLY TABLES (tm) are necessarily
> > for, so I left them as is.  Sorry, Roman. :-P
> > 
> 
> OK, sorry, I messed something up but fixed it.  In raw DV, I had
> forgotten to modify dv_inject_video to copy header metadata that is
> set in the codec rather than skip over it.  New patch is attached.
> 
> plain text document attachment (dv-metadata.patch)
> --- libavcodec/dv.c.orig	2006-08-21 13:34:56.000000000 -0500
> +++ libavcodec/dv.c	2006-08-22 23:12:51.453125000 -0500
> @@ -61,6 +61,34 @@
>  
>  static void* dv_anchor[DV_ANCHOR_SIZE];
>  
> +static const int dv_vaux_packs_dist[12][15] = {
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> +      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> +};
> +
> +
>  #define TEX_VLC_BITS 9
>  
>  #ifdef DV_CODEC_TINY_TARGET
> @@ -247,6 +275,92 @@
>      return 0;
>  }
>  
> +static int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c, uint8_t* buf)
> +{
> +    uint8_t aspect = 0;
> +
> +    /* Its hard to tell what SMPTE requires w.r.t. APT, but Quicktime needs it.
> +     * We set it based on pix_fmt value but it really should be per DV profile */
> +    int apt = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 1 : 0);
> +
> +    if((int)(av_q2d(c->avctx->sample_aspect_ratio) * c->avctx->width / c->avctx->height * 10) == 17) /* 16:9 */
> +        aspect = 0x02;
> +
> +    buf[0] = (uint8_t)pack_id;
> +    switch (pack_id) {
> +    case dv_header525: /* I can't imagine why these two weren't defined as real */
> +    case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
> +          buf[1] = 0xf8 |               /* reserved -- always 1 */
> +                   (apt & 0x07);        /* APT: Track application ID */
> +          buf[2] = (0 << 7)    | /* TF1: audio data is 0 - valid; 1 - invalid */
> +                   (0x0f << 3) | /* reserved -- always 1 */
> +                   (apt & 0x07); /* AP1: Audio application ID */
> +          buf[3] = (0 << 7)    | /* TF2: video data is 0 - valid; 1 - invalid */
> +                   (0x0f << 3) | /* reserved -- always 1 */
> +                   (apt & 0x07); /* AP2: Video application ID */
> +          buf[4] = (0 << 7)    | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
> +                   (0x0f << 3) | /* reserved -- always 1 */
> +                   (apt & 0x07); /* AP3: Subcode application ID */
> +          break;
> +    case dv_video_source:
> +          buf[1] = 0xff; /* reserved -- always 1 */
> +          buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
> +                   (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
> +                   (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
> +                   0xf; /* reserved -- always 1 */
> +          buf[3] = (3 << 6) | /* reserved -- always 1 */
> +                   (c->sys->dsf << 5) | /*  system: 60fields/50fields */
> +                   (apt << 2); /* signal type video compression */
> +          buf[4] = 0xff; /* VISC: 0xff -- no information */
> +          break;
> +    case dv_video_control:
> +          buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
> +                   0x3f; /* reserved -- always 1 */
> +          buf[2] = 0xc8 | /* reserved -- always b11001xxx */
> +                   aspect;
> +          buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */
> +                   (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */
> +                   (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */
> +                   (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
> +                   0xc; /* reserved -- always b1100 */
> +          buf[4] = 0xff; /* reserved -- always 1 */
> +          break;
> +    default:
> +          buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
> +    }
> +    return 5;
> +}
> +
> +static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
> +{
> +    int chan, i, j, k;
> +
> +    for (chan = 0; chan < c->sys->n_difchan; chan++) {
> +        for (i = 0; i < c->sys->difseg_size; i++) {
> +            memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
> +
> +            /* DV header: 1DIF */
> +            buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
> +            buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
> +            buf += 72; /* unused bytes */
> +
> +            /* Skip DV subcode for now: 2DIFs */
> +            buf += 80 * 2;
> +
> +            /* DV VAUX: 3DIFS */
> +            for (j = 0; j < 3; j++) {
> +                buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
> +                for (k = 0; k < 15 ; k++)
> +                    buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf);
> +                buf += 2; /* unused bytes */
> +            }
> +
> +            /* Skip audio and video sections */
> +            buf += 80 * 144;
> +        }
> +    }
> +}
> +
>  // #define VLC_DEBUG
>  // #define printf(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__)
>  
> @@ -1102,6 +1216,8 @@
>         fills in ALL of the metadata - e.g. for Quicktime-wrapped DV
>         streams) */
>  
> +    dv_format_frame(s, buf);
> +
>      /* NTSC/PAL format */
>      buf[3] = s->sys->dsf ? 0x80 : 0x00;
>  
> --- libavcodec/dvdata.h.orig	2006-08-21 13:34:58.000000000 -0500
> +++ libavcodec/dvdata.h	2006-08-22 23:12:51.468750000 -0500
> @@ -2624,6 +2624,29 @@
>      }
>  };
>  
> +enum dv_section_type {
> +     dv_sect_header  = 0x1f,
> +     dv_sect_subcode = 0x3f,
> +     dv_sect_vaux    = 0x56,
> +     dv_sect_audio   = 0x76,
> +     dv_sect_video   = 0x96,
> +};
> +
> +enum dv_pack_type {
> +     dv_header525     = 0x3f, /* see dv_write_pack for important details on */
> +     dv_header625     = 0xbf, /* these two packs */
> +     dv_timecode      = 0x13,
> +     dv_audio_source  = 0x50,
> +     dv_audio_control = 0x51,
> +     dv_audio_recdate = 0x52,
> +     dv_audio_rectime = 0x53,
> +     dv_video_source  = 0x60,
> +     dv_video_control = 0x61,
> +     dv_video_recdate = 0x62,
> +     dv_video_rectime = 0x63,
> +     dv_unknown_pack  = 0xff,
> +};
> +
>  /* minimum number of bytes to read from a DV stream in order to determine the profile */
>  #define DV_PROFILE_BYTES (6*80) /* 6 DIF blocks */
>  
> @@ -2663,3 +2686,14 @@
>  
>      return NULL;
>  }
> +
> +static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num, uint8_t seq_num,
> +                                  uint8_t dif_num, uint8_t* buf)
> +{
> +    buf[0] = (uint8_t)t;    /* Section type */
> +    buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
> +             (chan_num << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
> +             7;             /* reserved -- always 1 */
> +    buf[2] = dif_num;       /* DIF block number Video: 0-134, Audio: 0-8 */
> +    return 3;
> +}
> --- libavformat/dv.c.orig	2006-08-21 13:35:06.000000000 -0500
> +++ libavformat/dv.c	2006-08-22 23:30:17.500000000 -0500
> @@ -55,30 +55,6 @@
>      uint8_t     frame_buf[DV_MAX_FRAME_SIZE]; /* frame under contruction */
>  };
>  
> -enum dv_section_type {
> -     dv_sect_header  = 0x1f,
> -     dv_sect_subcode = 0x3f,
> -     dv_sect_vaux    = 0x56,
> -     dv_sect_audio   = 0x76,
> -     dv_sect_video   = 0x96,
> -};
> -
> -enum dv_pack_type {
> -     dv_header525     = 0x3f, /* see dv_write_pack for important details on */
> -     dv_header625     = 0xbf, /* these two packs */
> -     dv_timecode      = 0x13,
> -     dv_audio_source  = 0x50,
> -     dv_audio_control = 0x51,
> -     dv_audio_recdate = 0x52,
> -     dv_audio_rectime = 0x53,
> -     dv_video_source  = 0x60,
> -     dv_video_control = 0x61,
> -     dv_video_recdate = 0x62,
> -     dv_video_rectime = 0x63,
> -     dv_unknown_pack  = 0xff,
> -};
> -
> -
>  
>  /*
>   * The reason why the following three big ugly looking tables are
> @@ -109,33 +85,6 @@
>      { 0x13, 0x62, 0x63, 0x13, 0x62, 0x63 },
>  };
>  
> -static const int dv_vaux_packs_dist[12][15] = {
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -    { 0x60, 0x61, 0x62, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff,
> -      0x60, 0x61, 0x62, 0x63, 0xff, 0xff },
> -};
> -
>  static const int dv_aaux_packs_dist[12][9] = {
>      { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
>      { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
> @@ -189,20 +138,6 @@
>  
>      buf[0] = (uint8_t)pack_id;
>      switch (pack_id) {
> -    case dv_header525: /* I can't imagine why these two weren't defined as real */
> -    case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
> -          buf[1] = 0xf8 |               /* reserved -- always 1 */
> -                   (apt & 0x07);        /* APT: Track application ID */
> -          buf[2] = (0 << 7)    | /* TF1: audio data is 0 - valid; 1 - invalid */
> -                   (0x0f << 3) | /* reserved -- always 1 */
> -                   (apt & 0x07); /* AP1: Audio application ID */
> -          buf[3] = (0 << 7)    | /* TF2: video data is 0 - valid; 1 - invalid */
> -                   (0x0f << 3) | /* reserved -- always 1 */
> -                   (apt & 0x07); /* AP2: Video application ID */
> -          buf[4] = (0 << 7)    | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
> -                   (0x0f << 3) | /* reserved -- always 1 */
> -                   (apt & 0x07); /* AP3: Subcode application ID */
> -          break;
>      case dv_timecode:
>            ct = (time_t)(c->frames / ((float)c->sys->frame_rate /
>                                       (float)c->sys->frame_rate_base));
> @@ -293,46 +228,19 @@
>                     ((tc.tm_hour / 10) << 4) | /* Tens of hours */
>                     (tc.tm_hour % 10);         /* Units of hours */
>            break;
> -    case dv_video_source:
> -          buf[1] = 0xff; /* reserved -- always 1 */
> -          buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
> -                   (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
> -                   (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
> -                   0xf; /* reserved -- always 1 */
> -          buf[3] = (3 << 6) | /* reserved -- always 1 */
> -                   (c->sys->dsf << 5) | /*  system: 60fields/50fields */
> -                   (apt << 2); /* signal type video compression */
> -          buf[4] = 0xff; /* VISC: 0xff -- no information */
> -          break;
> -    case dv_video_control:
> -          buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
> -                   0x3f; /* reserved -- always 1 */
> -          buf[2] = 0xc8 | /* reserved -- always b11001xxx */
> -                   c->aspect;
> -          buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */
> -                   (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */
> -                   (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */
> -                   (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
> -                   0xc; /* reserved -- always b1100 */
> -          buf[4] = 0xff; /* reserved -- always 1 */
> -          break;
>      default:
> -          buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
> +          /* Don't overwrite previous packs */
> +          switch(pack_id) {
> +             case dv_header525: case dv_header625: case dv_video_source: case dv_video_control:
> +                break;
> +             default:
> +               buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
> +          }
> +
>      }
>      return 5;
>  }
>  
> -static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num, uint8_t seq_num,
> -                                  uint8_t dif_num, uint8_t* buf)
> -{
> -    buf[0] = (uint8_t)t;    /* Section type */
> -    buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
> -             (chan_num << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
> -             7;             /* reserved -- always 1 */
> -    buf[2] = dif_num;       /* DIF block number Video: 0-134, Audio: 0-8 */
> -    return 3;
> -}
> -
>  static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
>  {
>      if (syb_num == 0 || syb_num == 6) {
> @@ -361,12 +269,8 @@
>  
>      for (chan = 0; chan < c->sys->n_difchan; chan++) {
>          for (i = 0; i < c->sys->difseg_size; i++) {
> -            memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
> -
>              /* DV header: 1DIF */
> -            buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
> -            buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
> -            buf += 72; /* unused bytes */
> +            buf += 80;
>  
>              /* DV subcode: 2DIFs */
>              for (j = 0; j < 2; j++) {
> @@ -379,12 +283,7 @@
>              }
>  
>              /* DV VAUX: 3DIFs */
> -            for (j = 0; j < 3; j++) {
> -                buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
> -                for (k = 0; k < 15 ; k++)
> -                    buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf);
> -                buf += 2; /* unused bytes */
> -            }
> +            buf += 80 * 3;
>  
>              /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
>              for (j = 0; j < 135; j++) {
> @@ -432,7 +331,10 @@
>  
>      for (chan = 0; chan < c->sys->n_difchan; chan++) {
>          for (i = 0; i < c->sys->difseg_size; i++) {
> -            ptr += 6 * 80; /* skip DIF segment header */
> +            memcpy(frame_ptr + ptr, video_data + ptr, 80);
> +            ptr += 3 * 80;
> +            memcpy(frame_ptr + ptr, video_data + ptr, 3 * 80);
> +            ptr += 3 * 80;
>              for (j = 0; j < 135; j++) {
>                  if (j%15 == 0)
>                      ptr += 80; /* skip Audio DIF */
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at mplayerhq.hu
> http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-devel





More information about the ffmpeg-devel mailing list