]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/flacenc.c
avconv: drop update_sample_fmt()
[ffmpeg] / libavcodec / flacenc.c
index 1699312c8cca4effea5b64f77755a8e0d30e30f5..804333d5e9cbdb2056e22413557058b7f4ed4133 100644 (file)
@@ -24,7 +24,7 @@
 #include "libavutil/md5.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
-#include "dsputil.h"
+#include "bswapdsp.h"
 #include "get_bits.h"
 #include "golomb.h"
 #include "internal.h"
@@ -67,6 +67,7 @@ typedef struct RiceContext {
     enum CodingMode coding_mode;
     int porder;
     int params[MAX_PARTITIONS];
+    uint32_t udata[FLAC_MAX_BLOCKSIZE];
 } RiceContext;
 
 typedef struct FlacSubframe {
@@ -112,8 +113,11 @@ typedef struct FlacEncodeContext {
     struct AVMD5 *md5ctx;
     uint8_t *md5_buffer;
     unsigned int md5_buffer_size;
-    DSPContext dsp;
+    BswapDSPContext bdsp;
     FLACDSPContext flac_dsp;
+
+    int flushed;
+    int64_t next_pts;
 } FlacEncodeContext;
 
 
@@ -397,7 +401,7 @@ static av_cold int flac_encode_init(AVCodecContext *avctx)
     ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
                       s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
 
-    ff_dsputil_init(&s->dsp, avctx);
+    ff_bswapdsp_init(&s->bdsp);
     ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt,
                     avctx->bits_per_raw_sample);
 
@@ -613,8 +617,7 @@ static uint64_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
     uint64_t bits[MAX_PARTITION_ORDER+1];
     int opt_porder;
     RiceContext tmp_rc;
-    uint32_t *udata;
-    uint64_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
+    uint64_t sums[MAX_PARTITION_ORDER + 1][MAX_PARTITIONS] = { { 0 } };
 
     assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
     assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
@@ -622,11 +625,10 @@ static uint64_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
 
     tmp_rc.coding_mode = rc->coding_mode;
 
-    udata = av_malloc(n * sizeof(uint32_t));
     for (i = 0; i < n; i++)
-        udata[i] = (2*data[i]) ^ (data[i]>>31);
+        rc->udata[i] = (2 * data[i]) ^ (data[i] >> 31);
 
-    calc_sums(pmin, pmax, udata, n, pred_order, sums);
+    calc_sums(pmin, pmax, rc->udata, n, pred_order, sums);
 
     opt_porder = pmin;
     bits[pmin] = UINT32_MAX;
@@ -638,7 +640,6 @@ static uint64_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
         }
     }
 
-    av_freep(&udata);
     return bits[opt_porder];
 }
 
@@ -1176,8 +1177,8 @@ static int update_md5_sum(FlacEncodeContext *s, const void *samples)
     if (s->avctx->bits_per_raw_sample <= 16) {
         buf = (const uint8_t *)samples;
 #if HAVE_BIGENDIAN
-        s->dsp.bswap16_buf((uint16_t *)s->md5_buffer,
-                           (const uint16_t *)samples, buf_size / 2);
+        s->bdsp.bswap16_buf((uint16_t *) s->md5_buffer,
+                            (const uint16_t *) samples, buf_size / 2);
         buf = s->md5_buffer;
 #endif
     } else {
@@ -1212,6 +1213,20 @@ static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         s->max_framesize = s->max_encoded_framesize;
         av_md5_final(s->md5ctx, s->md5sum);
         write_streaminfo(s, avctx->extradata);
+
+        if (avctx->side_data_only_packets && !s->flushed) {
+            uint8_t *side_data = av_packet_new_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
+                                                         avctx->extradata_size);
+            if (!side_data)
+                return AVERROR(ENOMEM);
+            memcpy(side_data, avctx->extradata, avctx->extradata_size);
+
+            avpkt->pts = s->next_pts;
+
+            *got_packet_ptr = 1;
+            s->flushed = 1;
+        }
+
         return 0;
     }
 
@@ -1232,7 +1247,7 @@ static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
     frame_bytes = encode_frame(s);
 
-    /* fallback to verbatim mode if the compressed frame is larger than it
+    /* Fall back on verbatim mode if the compressed frame is larger than it
        would be if encoded uncompressed. */
     if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
         s->frame.verbatim_only = 1;
@@ -1264,6 +1279,9 @@ static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     avpkt->pts      = frame->pts;
     avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
     avpkt->size     = out_bytes;
+
+    s->next_pts = avpkt->pts + avpkt->duration;
+
     *got_packet_ptr = 1;
     return 0;
 }
@@ -1318,6 +1336,7 @@ static const AVClass flac_encoder_class = {
 
 AVCodec ff_flac_encoder = {
     .name           = "flac",
+    .long_name      = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
     .type           = AVMEDIA_TYPE_AUDIO,
     .id             = AV_CODEC_ID_FLAC,
     .priv_data_size = sizeof(FlacEncodeContext),
@@ -1328,6 +1347,5 @@ AVCodec ff_flac_encoder = {
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                      AV_SAMPLE_FMT_S32,
                                                      AV_SAMPLE_FMT_NONE },
-    .long_name      = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
     .priv_class     = &flac_encoder_class,
 };