]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/wmaenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / wmaenc.c
index 6ec6d7ce57b70b6f7636621aa41744ac5fc0f78f..71446e1ce1e20076da020e5049ea1d40688e9fbb 100644 (file)
@@ -20,7 +20,9 @@
  */
 
 #include "avcodec.h"
+#include "internal.h"
 #include "wma.h"
+#include "libavutil/avassert.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -39,6 +41,12 @@ static int encode_init(AVCodecContext * avctx){
         return AVERROR(EINVAL);
     }
 
+    if (avctx->sample_rate > 48000) {
+        av_log(avctx, AV_LOG_ERROR, "sample rate is too high: %d > 48kHz",
+               avctx->sample_rate);
+        return AVERROR(EINVAL);
+    }
+
     if(avctx->bit_rate < 24*1000) {
         av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\n",
                avctx->bit_rate);
@@ -64,6 +72,8 @@ static int encode_init(AVCodecContext * avctx){
     s->use_exp_vlc = flags2 & 0x0001;
     s->use_bit_reservoir = flags2 & 0x0002;
     s->use_variable_block_len = flags2 & 0x0004;
+    if (avctx->channels == 2)
+        s->ms_stereo = 1;
 
     ff_wma_init(avctx, flags2);
 
@@ -71,10 +81,19 @@ static int encode_init(AVCodecContext * avctx){
     for(i = 0; i < s->nb_block_sizes; i++)
         ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);
 
-    avctx->block_align=
-    s->block_align= avctx->bit_rate*(int64_t)s->frame_len / (avctx->sample_rate*8);
+    s->block_align     = avctx->bit_rate * (int64_t)s->frame_len /
+                         (avctx->sample_rate * 8);
+    s->block_align     = FFMIN(s->block_align, MAX_CODED_SUPERFRAME_SIZE);
+    avctx->block_align = s->block_align;
+    avctx->bit_rate    = avctx->block_align * 8LL * avctx->sample_rate /
+                         s->frame_len;
 //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d\n", s->block_align, avctx->bit_rate, s->frame_len, avctx->sample_rate);
-    avctx->frame_size= s->frame_len;
+    avctx->frame_size = avctx->delay = s->frame_len;
+
+#if FF_API_OLD_ENCODE_AUDIO
+    avctx->coded_frame = &s->frame;
+    avcodec_get_frame_defaults(avctx->coded_frame);
+#endif
 
     return 0;
 }
@@ -181,7 +200,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
     }
 
     if (s->nb_channels == 2) {
-        put_bits(&s->pb, 1, s->ms_stereo= 1);
+        put_bits(&s->pb, 1, !!s->ms_stereo);
     }
 
     for(ch = 0; ch < s->nb_channels; ch++) {
@@ -294,10 +313,6 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
                         if(1<<coef_nb_bits <= abs_level)
                             return -1;
 
-
-                        //Workaround minor rounding differences for the regression tests, FIXME we should find and replace the problematic float by fixpoint for reg tests
-                        if(abs_level == 0x71B && (s->avctx->flags & CODEC_FLAG_BITEXACT)) abs_level=0x71A;
-
                         put_bits(&s->pb, coef_nb_bits, abs_level);
                         put_bits(&s->pb, s->frame_len_bits, run);
                     }
@@ -332,16 +347,17 @@ static int encode_frame(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
     return put_bits_count(&s->pb)/8 - s->block_align;
 }
 
-static int encode_superframe(AVCodecContext *avctx,
-                            unsigned char *buf, int buf_size, void *data){
+static int encode_superframe(AVCodecContext *avctx, AVPacket *avpkt,
+                             const AVFrame *frame, int *got_packet_ptr)
+{
     WMACodecContext *s = avctx->priv_data;
-    const short *samples = data;
-    int i, total_gain;
+    const int16_t *samples = (const int16_t *)frame->data[0];
+    int i, total_gain, ret;
 
     s->block_len_bits= s->frame_len_bits; //required by non variable block len
     s->block_len = 1 << s->block_len_bits;
 
-    apply_window_and_mdct(avctx, samples, avctx->frame_size);
+    apply_window_and_mdct(avctx, samples, frame->nb_samples);
 
     if (s->ms_stereo) {
         float a, b;
@@ -355,19 +371,23 @@ static int encode_superframe(AVCodecContext *avctx,
         }
     }
 
+    if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE)))
+        return ret;
+
 #if 1
     total_gain= 128;
     for(i=64; i; i>>=1){
-        int error= encode_frame(s, s->coefs, buf, buf_size, total_gain-i);
+        int error = encode_frame(s, s->coefs, avpkt->data, avpkt->size,
+                                 total_gain - i);
         if(error<0)
             total_gain-= i;
     }
 #else
     total_gain= 90;
-    best= encode_frame(s, s->coefs, buf, buf_size, total_gain);
+    best = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain);
     for(i=32; i; i>>=1){
-        int scoreL= encode_frame(s, s->coefs, buf, buf_size, total_gain-i);
-        int scoreR= encode_frame(s, s->coefs, buf, buf_size, total_gain+i);
+        int scoreL = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain - i);
+        int scoreR = encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain + i);
         av_log(NULL, AV_LOG_ERROR, "%d %d %d (%d)\n", scoreL, best, scoreR, total_gain);
         if(scoreL < FFMIN(best, scoreR)){
             best = scoreL;
@@ -379,15 +399,22 @@ static int encode_superframe(AVCodecContext *avctx,
     }
 #endif
 
-    encode_frame(s, s->coefs, buf, buf_size, total_gain);
-    assert((put_bits_count(&s->pb) & 7) == 0);
+    encode_frame(s, s->coefs, avpkt->data, avpkt->size, total_gain);
+    av_assert0((put_bits_count(&s->pb) & 7) == 0);
     i= s->block_align - (put_bits_count(&s->pb)+7)/8;
-    assert(i>=0);
+    av_assert0(i>=0);
     while(i--)
         put_bits(&s->pb, 8, 'N');
 
     flush_put_bits(&s->pb);
-    return put_bits_ptr(&s->pb) - s->pb.buf;
+    av_assert0(put_bits_ptr(&s->pb) - s->pb.buf == s->block_align);
+
+    if (frame->pts != AV_NOPTS_VALUE)
+        avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
+
+    avpkt->size = s->block_align;
+    *got_packet_ptr = 1;
+    return 0;
 }
 
 AVCodec ff_wmav1_encoder = {
@@ -396,9 +423,10 @@ AVCodec ff_wmav1_encoder = {
     .id             = CODEC_ID_WMAV1,
     .priv_data_size = sizeof(WMACodecContext),
     .init           = encode_init,
-    .encode         = encode_superframe,
+    .encode2        = encode_superframe,
     .close          = ff_wma_end,
-    .sample_fmts    = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
+    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
+                                                     AV_SAMPLE_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
 };
 
@@ -408,8 +436,9 @@ AVCodec ff_wmav2_encoder = {
     .id             = CODEC_ID_WMAV2,
     .priv_data_size = sizeof(WMACodecContext),
     .init           = encode_init,
-    .encode         = encode_superframe,
+    .encode2        = encode_superframe,
     .close          = ff_wma_end,
-    .sample_fmts    = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
+    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
+                                                     AV_SAMPLE_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
 };