[FFmpeg-devel] [PATCH] avformat/dvenc: support muxing dvcprohd

Baptiste Coudurier baptiste.coudurier at gmail.com
Wed Nov 13 19:22:20 EET 2019


---
 libavformat/dvenc.c | 48 +++++++++++++++++++++------------------------
 1 file changed, 22 insertions(+), 26 deletions(-)

diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c
index 93c103b316..a7d1413eb2 100644
--- a/libavformat/dvenc.c
+++ b/libavformat/dvenc.c
@@ -28,7 +28,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include <time.h>
-#include <stdarg.h>
 
 #include "avformat.h"
 #include "internal.h"
@@ -48,8 +47,8 @@ struct DVMuxContext {
     AVClass          *av_class;
     const AVDVProfile*  sys;           /* current DV profile, e.g.: 525/60, 625/50 */
     int               n_ast;         /* number of stereo audio streams (up to 2) */
-    AVStream         *ast[2];        /* stereo audio streams */
-    AVFifoBuffer     *audio_data[2]; /* FIFO for storing excessive amounts of PCM */
+    AVStream         *ast[4];        /* stereo audio streams */
+    AVFifoBuffer     *audio_data[4]; /* FIFO for storing excessive amounts of PCM */
     int               frames;        /* current frame number */
     int64_t           start_time;    /* recording start time */
     int               has_audio;     /* frame under construction has audio */
@@ -87,14 +86,12 @@ static int dv_audio_frame_size(const AVDVProfile* sys, int frame, int sample_rat
                                             sizeof(sys->audio_samples_dist[0]))];
 }
 
-static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf, ...)
+static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf, int channel, int seq)
 {
     struct tm tc;
     time_t ct;
     uint32_t timecode;
-    va_list ap;
     int audio_type = 0;
-    int channel;
 
     buf[0] = (uint8_t)pack_id;
     switch (pack_id) {
@@ -104,8 +101,6 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu
         AV_WB32(buf + 1, timecode);
         break;
     case dv_audio_source:  /* AAUX source pack */
-        va_start(ap, buf);
-        channel = va_arg(ap, int);
         if (c->ast[channel]->codecpar->sample_rate == 44100) {
             audio_type = 1;
         } else if (c->ast[channel]->codecpar->sample_rate == 32000)
@@ -118,17 +113,16 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu
         buf[2] = (0 << 7) | /* multi-stereo      */
                  (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
                  (0 << 4) | /* pair bit: 0 -- one pair of channels */
-                 !!va_arg(ap, int); /* audio mode        */
+                 (seq >= c->sys->difseg_size/2); /* audio mode (1st or 2nd channel) */
         buf[3] = (1 << 7) | /* res               */
                  (1 << 6) | /* multi-language flag */
                  (c->sys->dsf << 5) | /*  system: 60fields/50fields */
-                 (c->sys->n_difchan & 2); /* definition: 0 -- 25Mbps, 2 -- 50Mbps */
+                 (DV_PROFILE_IS_HD(c->sys) ? 0x3 : c->sys->video_stype ? 2 : 0); /* stype */
         buf[4] = (1 << 7) | /* emphasis: 1 -- off */
                  (0 << 6) | /* emphasis time constant: 0 -- reserved */
                  (audio_type << 3) | /* frequency: 0 -- 48kHz, 1 -- 44,1kHz, 2 -- 32kHz */
                   0;        /* quantization: 0 -- 16-bit linear, 1 -- 12-bit nonlinear */
 
-        va_end(ap);
         break;
     case dv_audio_control:
         buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
@@ -192,7 +186,7 @@ static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr)
     for (i = 0; i < c->sys->difseg_size; i++) {
         frame_ptr += 6 * 80; /* skip DIF segment header */
         for (j = 0; j < 9; j++) {
-            dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3], channel, i >= c->sys->difseg_size/2);
+            dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3], channel, i);
             for (d = 8; d < 80; d+=2) {
                 of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
                 if (of*2 >= size)
@@ -210,27 +204,28 @@ static void dv_inject_metadata(DVMuxContext *c, uint8_t* frame)
 {
     int j, k;
     uint8_t* buf;
+    int seq = 0;
 
-    for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80) {
+    for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80, seq++) {
         /* DV subcode: 2nd and 3d DIFs */
         for (j = 80; j < 80 * 3; j += 80) {
             for (k = 6; k < 6 * 8; k += 8)
-                dv_write_pack(dv_timecode, c, &buf[j+k]);
+                dv_write_pack(dv_timecode, c, &buf[j+k], 0, seq);
 
             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 ? */
-                dv_write_pack(dv_video_recdate, c, &buf[j+14]);
-                dv_write_pack(dv_video_rectime, c, &buf[j+22]);
-                dv_write_pack(dv_video_recdate, c, &buf[j+38]);
-                dv_write_pack(dv_video_rectime, c, &buf[j+46]);
+                dv_write_pack(dv_video_recdate, c, &buf[j+14], 0, seq);
+                dv_write_pack(dv_video_rectime, c, &buf[j+22], 0, seq);
+                dv_write_pack(dv_video_recdate, c, &buf[j+38], 0, seq);
+                dv_write_pack(dv_video_rectime, c, &buf[j+46], 0, seq);
             }
         }
 
         /* DV VAUX: 4th, 5th and 6th 3DIFs */
         for (j = 80*3 + 3; j < 80*6; j += 80) {
-            dv_write_pack(dv_video_recdate, c, &buf[j+5*2]);
-            dv_write_pack(dv_video_rectime, c, &buf[j+5*3]);
-            dv_write_pack(dv_video_recdate, c, &buf[j+5*11]);
-            dv_write_pack(dv_video_rectime, c, &buf[j+5*12]);
+            dv_write_pack(dv_video_recdate, c, &buf[j+5* 2], 0, seq);
+            dv_write_pack(dv_video_rectime, c, &buf[j+5* 3], 0, seq);
+            dv_write_pack(dv_video_recdate, c, &buf[j+5*11], 0, seq);
+            dv_write_pack(dv_video_rectime, c, &buf[j+5*12], 0, seq);
         }
     }
 }
@@ -307,11 +302,11 @@ static DVMuxContext* dv_init_mux(AVFormatContext* s)
     int i;
 
     /* we support at most 1 video and 2 audio streams */
-    if (s->nb_streams > 3)
+    if (s->nb_streams > 5)
         return NULL;
 
     c->n_ast  = 0;
-    c->ast[0] = c->ast[1] = NULL;
+    c->ast[0] = c->ast[1] = c->ast[2] = c->ast[3] = NULL;
 
     /* We have to sort out where audio and where video stream is */
     for (i=0; i<s->nb_streams; i++) {
@@ -355,8 +350,9 @@ static DVMuxContext* dv_init_mux(AVFormatContext* s)
             goto bail_out;
     }
 
-    if ((c->n_ast > 1) && (c->sys->n_difchan < 2)) {
-        /* only 1 stereo pair is allowed in 25Mbps mode */
+    if (((c->n_ast > 1) && (c->sys->n_difchan < 2)) ||
+        ((c->n_ast > 2) && (c->sys->n_difchan < 4))) {
+        /* only 2 stereo pairs allowed in 50Mbps mode */
         goto bail_out;
     }
 
-- 
2.21.0 (Apple Git-122)



More information about the ffmpeg-devel mailing list