[FFmpeg-devel] [PATCH]Addition of MLP encoder

Disha Singh directionstar86 at gmail.com
Wed Mar 23 17:00:41 CET 2016


This is the modified diff with few changes in mlp_encode_frame function.
-Disha


On Tue, Mar 22, 2016 at 1:59 AM, Disha Singh <directionstar86 at gmail.com>
wrote:

> Qualification task for project TrueHD encoder.
> There are two patches.
> One has the changes made to other files to support mlpenc.c and the other
> only has mlpenc.c.
> Also attached is the diff file of mlpenc.c.
> (Modified the file :
> https://github.com/ramiropolla/soc/blob/master/mlp/mlpenc.c)
>
-------------- next part --------------
diff --git a/mlpenc.c b/ffmpeg_sources/ffmpeg/libavcodec/mlpenc.c
old mode 100644
new mode 100755
index 70cb7d8..7035ad9
--- a/mlpenc.c
+++ b/ffmpeg_sources/ffmpeg/libavcodec/mlpenc.c
@@ -24,9 +24,15 @@
 #include "libavutil/crc.h"
 #include "libavutil/avstring.h"
 #include "mlp.h"
-#include "dsputil.h"
 #include "lpc.h"
 
+#include "libavutil/internal.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/channel_layout.h"
+#include "internal.h"
+#include "mlpdsp.h"
+#include "get_bits.h"
+
 #define MAJOR_HEADER_INTERVAL 16
 
 #define MLP_MIN_LPC_ORDER      1
@@ -97,6 +103,7 @@ typedef struct BestOffset {
 #define NUM_CODEBOOKS       4
 
 typedef struct {
+    const AVClass  *class;
     AVCodecContext *avctx;
 
     int             num_substreams;         ///< Number of substreams contained within this stream.
@@ -187,8 +194,9 @@ typedef struct {
     DecodingParams *seq_decoding_params;
 
     unsigned int    max_codebook_search;
+    
 
-    DSPContext      dsp;
+    MLPDSPContext      mlp_dsp;
 } MLPEncodeContext;
 
 static ChannelParams   restart_channel_params[MAX_CHANNELS];
@@ -368,10 +376,10 @@ static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
 static void copy_restart_frame_params(MLPEncodeContext *ctx,
                                       unsigned int substr)
 {
-    ChannelParams (*seq_cp)[ctx->avctx->channels] = (ChannelParams (*)[ctx->avctx->channels]) ctx->seq_channel_params;
-    DecodingParams (*seq_dp)[ctx->num_substreams] = (DecodingParams (*)[ctx->num_substreams]) ctx->seq_decoding_params;
     unsigned int index;
-
+    ChannelParams **seq_cp = &ctx->seq_channel_params;
+    DecodingParams **seq_dp = &ctx->seq_decoding_params;          
+    	
     for (index = 0; index < ctx->number_of_subblocks; index++) {
         DecodingParams *dp = &seq_dp[index][substr];
         unsigned int channel;
@@ -549,12 +557,12 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
     }
 
     switch (avctx->sample_fmt) {
-    case SAMPLE_FMT_S16:
+    case AV_SAMPLE_FMT_S16:
         ctx->coded_sample_fmt[0] = BITS_16;
         ctx->wordlength = 16;
         break;
     /* TODO 20 bits: */
-    case SAMPLE_FMT_S32:
+    case AV_SAMPLE_FMT_S32:
         ctx->coded_sample_fmt[0] = BITS_24;
         ctx->wordlength = 24;
         break;
@@ -565,7 +573,7 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
     }
     ctx->coded_sample_fmt[1] = -1 & 0xf;
 
-    avctx->coded_frame = avcodec_alloc_frame();
+    avctx->coded_frame = av_frame_alloc();
 
     ctx->dts = -avctx->frame_size;
 
@@ -622,8 +630,8 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
     ctx->channel_arrangement = avctx->channels - 1;
     ctx->num_substreams = 1;
     ctx->flags = FLAGS_DVDA;
-    ctx->channel_occupancy = ff_mlp_ch_info[avctx->channels - 1].channel_occupancy;
-    ctx->summary_info      = ff_mlp_ch_info[avctx->channels - 1].summary_info     ;
+    //ctx->channel_occupancy = ff_mlp_ch_info[avctx->channels - 1].channel_occupancy;
+    //ctx->summary_info      = ff_mlp_ch_info[avctx->channels - 1].summary_info     ;
 
     size = sizeof(unsigned int) * ctx->max_restart_interval;
 
@@ -680,7 +688,7 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx)
     clear_channel_params(ctx, restart_channel_params);
     clear_decoding_params(ctx, restart_decoding_params);
 
-    dsputil_init(&ctx->dsp, avctx);
+    ff_mlpdsp_init(&ctx->mlp_dsp);
 
     return 0;
 }
@@ -801,11 +809,12 @@ static unsigned int bitcount_decoding_params(MLPEncodeContext *ctx,
     return bitcount;
 }
 
+
 /****************************************************************************
  ****************** Functions that write to the bitstream *******************
  ****************************************************************************/
 
-/** Writes a major sync header to the bitstream. */
+/* Writes a major sync header to the bitstream. */
 static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
 {
     PutBitContext pb;
@@ -835,14 +844,14 @@ static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
     put_bits(&pb,  8, ctx->substream_info      );
     put_bits(&pb,  5, ctx->fs                  );
     put_bits(&pb,  5, ctx->wordlength          );
-    put_bits(&pb,  6, ctx->channel_occupancy   );
+    //put_bits(&pb,  6, ctx->channel_occupancy   );
     put_bits(&pb,  3, 0                        ); /* ignored */
     put_bits(&pb, 10, 0                        ); /* speaker_layout */
     put_bits(&pb,  3, 0                        ); /* copy_protection */
     put_bits(&pb, 16, 0x8080                   ); /* ignored */
     put_bits(&pb,  7, 0                        ); /* ignored */
     put_bits(&pb,  4, 0                        ); /* source_format */
-    put_bits(&pb,  5, ctx->summary_info        );
+    //put_bits(&pb,  5, ctx->summary_info        );
 
     flush_put_bits(&pb);
 
@@ -1232,7 +1241,7 @@ static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
                                       int buf_size, int restart_frame)
 {
     uint16_t substream_data_len[MAX_SUBSTREAMS];
-    uint8_t *buf2, *buf1, *buf0 = buf;
+    uint8_t *buf1, *buf0 = buf;
     unsigned int substr;
     int total_length;
 
@@ -1258,9 +1267,7 @@ static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
         buf      += 2;
         buf_size -= 2;
     }
-
-    buf2 = buf;
-
+    
     buf = write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
 
     total_length = buf - buf0;
@@ -1323,9 +1330,9 @@ static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
 }
 
 /** Wrapper function for inputting data in two different bit-depths. */
-static void input_data(MLPEncodeContext *ctx, void *samples)
+static void input_data(MLPEncodeContext *ctx, const uint8_t *samples)
 {
-    if (ctx->avctx->sample_fmt == SAMPLE_FMT_S32)
+    if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
         input_data_internal(ctx, samples, 1);
     else
         input_data_internal(ctx, samples, 0);
@@ -1458,7 +1465,7 @@ static void set_filter_params(MLPEncodeContext *ctx,
             sample_buffer += ctx->num_channels;
         }
 
-        order = ff_lpc_calc_coefs(&ctx->dsp, ctx->lpc_sample_buffer, ctx->number_of_samples,
+        order = ff_lpc_calc_coefs((LPCContext *)&ctx->mlp_dsp, ctx->lpc_sample_buffer, ctx->number_of_samples,
                                   MLP_MIN_LPC_ORDER, max_order, 11,
                                   coefs, shift, 1,
                                   ORDER_METHOD_EST, MLP_MIN_LPC_SHIFT, MLP_MAX_LPC_SHIFT, MLP_MIN_LPC_SHIFT);
@@ -1869,13 +1876,17 @@ static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
 {
     FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],
                                       &ctx->cur_channel_params[channel].filter_params[IIR], };
-    int32_t filter_state_buffer[NUM_FILTERS][ctx->number_of_samples];
+    int32_t **filter_state_buffer = (int32_t**)av_malloc((NUM_FILTERS)*sizeof(int32_t*));
     int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
     int32_t *sample_buffer = ctx->sample_buffer + channel;
     unsigned int number_of_samples = ctx->number_of_samples;
     unsigned int filter_shift = fp[FIR]->shift;
     int filter;
     int i;
+    
+    for (i = 0; i < NUM_FILTERS; i++) {
+        filter_state_buffer[i] = (int32_t*)av_malloc((ctx->number_of_samples)*sizeof(int32_t));
+    }
 
     for (i = 0; i < 8; i++) {
         filter_state_buffer[FIR][i] = *sample_buffer;
@@ -1965,7 +1976,7 @@ static void rematrix_channels(MLPEncodeContext *ctx)
     maxchan = ctx->num_channels;
 
     for (mat = 0; mat < mp->count; mat++) {
-        unsigned int msb_mask_bits = (ctx->avctx->sample_fmt == SAMPLE_FMT_S16 ? 8 : 0) - mp->shift[mat];
+        unsigned int msb_mask_bits = (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 8 : 0) - mp->shift[mat];
         int32_t mask = MSB_MASK(msb_mask_bits);
         unsigned int outch = mp->outch[mat];
 
@@ -2045,7 +2056,7 @@ static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
 
 static void set_best_codebook(MLPEncodeContext *ctx)
 {
-    ChannelParams (*seq_cp)[ctx->avctx->channels] = (ChannelParams (*)[ctx->avctx->channels]) ctx->seq_channel_params;
+    ChannelParams **seq_cp = &ctx->seq_channel_params;
     DecodingParams *dp = ctx->cur_decoding_params;
     RestartHeader *rh = ctx->cur_restart_header;
     unsigned int channel;
@@ -2130,20 +2141,16 @@ static void set_best_codebook(MLPEncodeContext *ctx)
 static void set_major_params(MLPEncodeContext *ctx)
 {
     RestartHeader *rh = ctx->cur_restart_header;
-    ChannelParams (*channel_params)[ctx->sequence_size][ctx->avctx->channels] =
-                 (ChannelParams (*)[ctx->sequence_size][ctx->avctx->channels]) ctx->channel_params;
-    DecodingParams (*decoding_params)[ctx->sequence_size][ctx->num_substreams] =
-                  (DecodingParams (*)[ctx->sequence_size][ctx->num_substreams]) ctx->decoding_params;
+    ChannelParams **channel_params = &ctx->channel_params;
+    DecodingParams **decoding_params = &ctx->decoding_params;
     unsigned int index;
     unsigned int substr;
     uint8_t max_huff_lsbs = 0;
     uint8_t max_output_bits = 0;
-
+    
     for (substr = 0; substr < ctx->num_substreams; substr++) {
-        DecodingParams (*seq_dp)[ctx->num_substreams] =
-             (DecodingParams (*)[ctx->num_substreams]) &decoding_params[ctx->restart_intervals - 1][ctx->seq_offset[ctx->restart_intervals - 1]];
-        ChannelParams (*seq_cp)[ctx->avctx->channels] =
-             (ChannelParams (*)[ctx->avctx->channels]) &channel_params[ctx->restart_intervals - 1][ctx->seq_offset[ctx->restart_intervals - 1]];
+        DecodingParams **seq_dp = (DecodingParams**)&decoding_params[ctx->restart_intervals - 1][ctx->seq_offset[ctx->restart_intervals - 1]];
+        ChannelParams **seq_cp = (ChannelParams**)&channel_params[ctx->restart_intervals - 1][ctx->seq_offset[ctx->restart_intervals - 1]];
         unsigned int channel;
         for (index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
             memcpy(&ctx->major_decoding_params[index][substr], &seq_dp[index][substr], sizeof(DecodingParams));
@@ -2187,11 +2194,11 @@ static void set_major_params(MLPEncodeContext *ctx)
 
 static void analyze_sample_buffer(MLPEncodeContext *ctx)
 {
-    ChannelParams (*seq_cp)[ctx->avctx->channels] = (ChannelParams (*)[ctx->avctx->channels]) ctx->seq_channel_params;
-    DecodingParams (*seq_dp)[ctx->num_substreams] = (DecodingParams (*)[ctx->num_substreams]) ctx->seq_decoding_params;
+    ChannelParams **seq_cp = &ctx->seq_channel_params;
+    DecodingParams **seq_dp = &ctx->seq_decoding_params;
     unsigned int index;
     unsigned int substr;
-
+    
     for (substr = 0; substr < ctx->num_substreams; substr++) {
 
         ctx->cur_restart_header = &ctx->restart_header[substr];
@@ -2262,10 +2269,13 @@ static void process_major_frame(MLPEncodeContext *ctx)
 
 /****************************************************************************/
 
-static int mlp_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size,
-                            void *data)
+static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame,
+                            int *got_packet_ptr)
 {
     MLPEncodeContext *ctx = avctx->priv_data;
+    uint8_t *buf = avpkt->data;
+    int buf_size = avpkt->size;
+    uint8_t *data = frame->data;
     unsigned int bytes_written = 0;
     int restart_frame;
 
@@ -2314,10 +2324,10 @@ static int mlp_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size,
 
     if (ctx->min_restart_interval == ctx->max_restart_interval)
         ctx->write_buffer = ctx->sample_buffer;
-
     avctx->coded_frame->key_frame = restart_frame;
 
     bytes_written = write_access_unit(ctx, buf, buf_size, restart_frame);
+    avpkt->size = bytes_written;
 
     ctx->timestamp += ctx->frame_size[ctx->frame_index];
     ctx->dts       += ctx->frame_size[ctx->frame_index];
@@ -2328,7 +2338,7 @@ input_and_return:
         ctx->frame_size[ctx->frame_index] = avctx->frame_size;
         ctx->next_major_frame_size += avctx->frame_size;
         ctx->next_major_number_of_frames++;
-        input_data(ctx, data);
+        input_data(ctx, (const uint8_t *)data);
     } else if (!ctx->last_frame) {
         ctx->last_frame = ctx->inout_buffer;
     }
@@ -2336,17 +2346,15 @@ input_and_return:
     restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
 
     if (!restart_frame) {
-    ChannelParams (*channel_params)[ctx->sequence_size][ctx->avctx->channels] =
-                 (ChannelParams (*)[ctx->sequence_size][ctx->avctx->channels]) ctx->channel_params;
-    DecodingParams (*decoding_params)[ctx->sequence_size][ctx->num_substreams] =
-                  (DecodingParams (*)[ctx->sequence_size][ctx->num_substreams]) ctx->decoding_params;
+    ChannelParams **channel_params = &ctx->channel_params;
+    DecodingParams **decoding_params = &ctx->decoding_params;
     int seq_index;
 
     for (seq_index = 0;
          seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
          seq_index++) {
-        ChannelParams (*seq_cp)[ctx->avctx->channels] = &channel_params[(ctx->frame_index / ctx->min_restart_interval)][ctx->seq_offset[seq_index]];
-        DecodingParams (*seq_dp)[ctx->num_substreams] = &decoding_params[(ctx->frame_index / ctx->min_restart_interval)][ctx->seq_offset[seq_index]];
+        ChannelParams **seq_cp = (ChannelParams**)&channel_params[(ctx->frame_index / ctx->min_restart_interval)][ctx->seq_offset[seq_index]];
+        DecodingParams **seq_dp = (DecodingParams**)&decoding_params[(ctx->frame_index / ctx->min_restart_interval)][ctx->seq_offset[seq_index]];
         unsigned int number_of_samples = 0;
         unsigned int index;
 
@@ -2389,6 +2397,8 @@ input_and_return:
 
 no_data_left:
 
+    *got_packet_ptr = 1;
+
     return bytes_written;
 }
 
@@ -2408,15 +2418,24 @@ static av_cold int mlp_encode_close(AVCodecContext *avctx)
     return 0;
 }
 
-AVCodec mlp_encoder = {
-    "mlp",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_MLP,
-    sizeof(MLPEncodeContext),
-    mlp_encode_init,
-    mlp_encode_frame,
-    mlp_encode_close,
-    .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
-    .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_S32,SAMPLE_FMT_NONE},
-    .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
+static const AVClass mlp_encoder_class = {
+    .class_name = "MLP encoder",
+    .item_name  = av_default_item_name,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_mlp_encoder = {    
+    .name           = "mlp",
+    .long_name      = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing))"),
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = AV_CODEC_ID_MLP,
+    .priv_data_size = sizeof(MLPEncodeContext),
+    .init           = mlp_encode_init,
+    .encode2        = mlp_encode_frame,
+    .close          = mlp_encode_close,
+    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY,
+    .sample_fmts    = (enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
+                                                     AV_SAMPLE_FMT_S32,
+                                                     AV_SAMPLE_FMT_NONE },
+    .priv_class     = &mlp_encoder_class,
 };


More information about the ffmpeg-devel mailing list