]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/huffyuv.c
mp3dec: ask for 8khz switch point mp3s
[ffmpeg] / libavcodec / huffyuv.c
index cd7a87600e244cde0da29246be32b94ab39f6165..2a9ebe1b2c38830070d54a10d70c671fe8168ee1 100644 (file)
@@ -6,20 +6,20 @@
  * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
  * the algorithm used
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
  */
 
 #include "avcodec.h"
+#include "internal.h"
 #include "get_bits.h"
 #include "put_bits.h"
 #include "dsputil.h"
 #include "thread.h"
+#include "huffman.h"
 
 #define VLC_BITS 11
 
@@ -86,14 +88,16 @@ typedef struct HYuvContext {
 static const unsigned char classic_shift_luma[classic_shift_luma_table_size + FF_INPUT_BUFFER_PADDING_SIZE] = {
   34,36,35,69,135,232,9,16,10,24,11,23,12,16,13,10,14,8,15,8,
   16,8,17,20,16,10,207,206,205,236,11,8,10,21,9,23,8,8,199,70,
-  69,68, 0
+  69,68, 0,
+  0,0,0,0,0,0,0,0,
 };
 
 #define classic_shift_chroma_table_size 59
 static const unsigned char classic_shift_chroma[classic_shift_chroma_table_size + FF_INPUT_BUFFER_PADDING_SIZE] = {
   66,36,37,38,39,40,41,75,76,77,110,239,144,81,82,83,84,85,118,183,
   56,57,88,89,56,89,154,57,58,57,26,141,57,56,58,57,58,57,184,119,
-  214,245,116,83,82,49,80,79,78,77,44,75,41,40,39,38,37,36,34, 0
+  214,245,116,83,82,49,80,79,78,77,44,75,41,40,39,38,37,36,34, 0,
+  0,0,0,0,0,0,0,0,
 };
 
 static const unsigned char classic_add_luma[256] = {
@@ -135,7 +139,7 @@ static const unsigned char classic_add_chroma[256] = {
 };
 
 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst,
-                                      uint8_t *src, int w, int left)
+                                      const uint8_t *src, int w, int left)
 {
     int i;
     if (w < 32) {
@@ -157,25 +161,28 @@ static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst,
 }
 
 static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst,
-                                             uint8_t *src, int w,
-                                             int *red, int *green, int *blue)
+                                             const uint8_t *src, int w,
+                                             int *red, int *green, int *blue, int *alpha)
 {
     int i;
-    int r,g,b;
+    int r,g,b,a;
     r = *red;
     g = *green;
     b = *blue;
-
+    a = *alpha;
     for (i = 0; i < FFMIN(w, 4); i++) {
         const int rt = src[i * 4 + R];
         const int gt = src[i * 4 + G];
         const int bt = src[i * 4 + B];
+        const int at = src[i * 4 + A];
         dst[i * 4 + R] = rt - r;
         dst[i * 4 + G] = gt - g;
         dst[i * 4 + B] = bt - b;
+        dst[i * 4 + A] = at - a;
         r = rt;
         g = gt;
         b = bt;
+        a = at;
     }
 
     s->dsp.diff_bytes(dst + 16, src + 16, src + 12, w * 4 - 16);
@@ -183,6 +190,32 @@ static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst,
     *red   = src[(w - 1) * 4 + R];
     *green = src[(w - 1) * 4 + G];
     *blue  = src[(w - 1) * 4 + B];
+    *alpha = src[(w - 1) * 4 + A];
+}
+
+static inline void sub_left_prediction_rgb24(HYuvContext *s, uint8_t *dst, const uint8_t *src, int w, int *red, int *green, int *blue){
+    int i;
+    int r,g,b;
+    r = *red;
+    g = *green;
+    b = *blue;
+    for (i = 0; i < FFMIN(w,16); i++) {
+        const int rt = src[i*3 + 0];
+        const int gt = src[i*3 + 1];
+        const int bt = src[i*3 + 2];
+        dst[i*3 + 0] = rt - r;
+        dst[i*3 + 1] = gt - g;
+        dst[i*3 + 2] = bt - b;
+        r = rt;
+        g = gt;
+        b = bt;
+    }
+
+    s->dsp.diff_bytes(dst + 48, src + 48, src + 48 - 3, w*3 - 48);
+
+    *red   = src[(w - 1)*3 + 0];
+    *green = src[(w - 1)*3 + 1];
+    *blue  = src[(w - 1)*3 + 2];
 }
 
 static int read_len_table(uint8_t *dst, GetBitContext *gb)
@@ -223,66 +256,6 @@ static int generate_bits_table(uint32_t *dst, const uint8_t *len_table)
     return 0;
 }
 
-#if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
-typedef struct {
-    uint64_t val;
-    int name;
-} HeapElem;
-
-static void heap_sift(HeapElem *h, int root, int size)
-{
-    while (root * 2 + 1 < size) {
-        int child = root * 2 + 1;
-        if (child < size - 1 && h[child].val > h[child + 1].val)
-            child++;
-        if (h[root].val > h[child].val) {
-            FFSWAP(HeapElem, h[root], h[child]);
-            root = child;
-        } else
-            break;
-    }
-}
-
-static void generate_len_table(uint8_t *dst, const uint64_t *stats)
-{
-    HeapElem h[256];
-    int up[2*256];
-    int len[2*256];
-    int offset, i, next;
-    int size = 256;
-
-    for (offset = 1; ; offset <<= 1) {
-        for (i = 0; i < size; i++) {
-            h[i].name = i;
-            h[i].val = (stats[i] << 8) + offset;
-        }
-        for (i = size / 2 - 1; i >= 0; i--)
-            heap_sift(h, i, size);
-
-        for (next = size; next < size * 2 - 1; next++) {
-            // merge the two smallest entries, and put it back in the heap
-            uint64_t min1v = h[0].val;
-            up[h[0].name] = next;
-            h[0].val = INT64_MAX;
-            heap_sift(h, 0, size);
-            up[h[0].name] = next;
-            h[0].name = next;
-            h[0].val += min1v;
-            heap_sift(h, 0, size);
-        }
-
-        len[2 * size - 2] = 0;
-        for (i = 2 * size - 3; i >= size; i--)
-            len[i] = len[up[i]] + 1;
-        for (i = 0; i < size; i++) {
-            dst[i] = len[up[i]] + 1;
-            if (dst[i] >= 32) break;
-        }
-        if (i==size) break;
-    }
-}
-#endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
-
 static void generate_joint_tables(HYuvContext *s)
 {
     uint16_t symbols[1 << VLC_BITS];
@@ -379,7 +352,6 @@ static int read_huffman_tables(HYuvContext *s, const uint8_t *src, int length)
 
 static int read_old_huffman_tables(HYuvContext *s)
 {
-#if 1
     GetBitContext gb;
     int i;
 
@@ -412,10 +384,6 @@ static int read_old_huffman_tables(HYuvContext *s)
     generate_joint_tables(s);
 
     return 0;
-#else
-    av_log(s->avctx, AV_LOG_DEBUG, "v1 huffyuv is not supported \n");
-    return -1;
-#endif
 }
 
 static av_cold void alloc_temp(HYuvContext *s)
@@ -442,7 +410,7 @@ static av_cold int common_init(AVCodecContext *avctx)
 
     s->width = avctx->width;
     s->height = avctx->height;
-    assert(s->width>0 && s->height>0);
+    av_assert1(s->width > 0 && s->height > 0);
 
     return 0;
 }
@@ -456,6 +424,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
     memset(s->vlc, 0, 3 * sizeof(VLC));
 
     avctx->coded_frame = &s->picture;
+    avcodec_get_frame_defaults(&s->picture);
     s->interlaced = s->height > 288;
 
     s->bgr32 = 1;
@@ -541,6 +510,11 @@ static av_cold int decode_init(AVCodecContext *avctx)
         return AVERROR_INVALIDDATA;
     }
 
+    if ((avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P) && avctx->width & 1) {
+        av_log(avctx, AV_LOG_ERROR, "width must be even for this colorspace\n");
+        return AVERROR_INVALIDDATA;
+    }
+
     alloc_temp(s);
 
     return 0;
@@ -583,8 +557,8 @@ static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf)
         for (; i < 256 && len[i] == val && repeat < 255; i++)
             repeat++;
 
-        assert(val < 32 && val >0 && repeat<256 && repeat>0);
-        if ( repeat > 7) {
+        av_assert0(val < 32 && val >0 && repeat<256 && repeat>0);
+        if (repeat > 7) {
             buf[index++] = val;
             buf[index++] = repeat;
         } else {
@@ -610,12 +584,17 @@ static av_cold int encode_init(AVCodecContext *avctx)
 
     switch (avctx->pix_fmt) {
     case PIX_FMT_YUV420P:
-        s->bitstream_bpp = 12;
-        break;
     case PIX_FMT_YUV422P:
-        s->bitstream_bpp = 16;
+        if (s->width & 1) {
+            av_log(avctx, AV_LOG_ERROR, "width must be even for this colorspace\n");
+            return AVERROR(EINVAL);
+        }
+        s->bitstream_bpp = avctx->pix_fmt == PIX_FMT_YUV420P ? 12 : 16;
         break;
     case PIX_FMT_RGB32:
+        s->bitstream_bpp = 32;
+        break;
+    case PIX_FMT_RGB24:
         s->bitstream_bpp = 24;
         break;
     default:
@@ -697,7 +676,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
     }
 
     for (i = 0; i < 3; i++) {
-        generate_len_table(s->len[i], s->stats[i]);
+        ff_huff_gen_len_table(s->len[i], s->stats[i]);
 
         if (generate_bits_table(s->bits[i], s->len[i]) < 0) {
             return -1;
@@ -919,27 +898,30 @@ static void decode_bgr_bitstream(HYuvContext *s, int count)
     }
 }
 
-static int encode_bgr_bitstream(HYuvContext *s, int count)
+static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
 {
     int i;
 
-    if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 3 * 4 * count) {
+    if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 4*planes*count) {
         av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
         return -1;
     }
 
 #define LOAD3\
-            int g =  s->temp[0][4 * i + G];\
-            int b = (s->temp[0][4 * i + B] - g) & 0xff;\
-            int r = (s->temp[0][4 * i + R] - g) & 0xff;
+            int g =  s->temp[0][planes==3 ? 3*i + 1 : 4*i + G];\
+            int b = (s->temp[0][planes==3 ? 3*i + 2 : 4*i + B] - g) & 0xff;\
+            int r = (s->temp[0][planes==3 ? 3*i + 0 : 4*i + R] - g) & 0xff;\
+            int a =  s->temp[0][planes*i + A];
 #define STAT3\
             s->stats[0][b]++;\
             s->stats[1][g]++;\
-            s->stats[2][r]++;
+            s->stats[2][r]++;\
+            if(planes==4) s->stats[2][a]++;
 #define WRITE3\
             put_bits(&s->pb, s->len[1][g], s->bits[1][g]);\
             put_bits(&s->pb, s->len[0][b], s->bits[0][b]);\
-            put_bits(&s->pb, s->len[2][r], s->bits[2][r]);
+            put_bits(&s->pb, s->len[2][r], s->bits[2][r]);\
+            if(planes==4) put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
 
     if ((s->flags & CODEC_FLAG_PASS1) &&
         (s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)) {
@@ -1295,11 +1277,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     AVFrame * const p = &s->picture;
     int i, j, size = 0, ret;
 
-    if (!pkt->data &&
-        (ret = av_new_packet(pkt, width * height * 3 * 4 + FF_MIN_BUFFER_SIZE)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Error allocating output packet.\n");
+    if ((ret = ff_alloc_packet2(avctx, pkt, width * height * 3 * 4 + FF_MIN_BUFFER_SIZE)) < 0)
         return ret;
-    }
 
     *p = *pict;
     p->pict_type = AV_PICTURE_TYPE_I;
@@ -1307,7 +1286,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 
     if (s->context) {
         for (i = 0; i < 3; i++) {
-            generate_len_table(s->len[i], s->stats[i]);
+            ff_huff_gen_len_table(s->len[i], s->stats[i]);
             if (generate_bits_table(s->bits[i], s->len[i]) < 0)
                 return -1;
             size += store_table(s, s->len[i], &pkt->data[size]);
@@ -1430,25 +1409,50 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         const int stride = -p->linesize[0];
         const int fake_stride = -fake_ystride;
         int y;
-        int leftr, leftg, leftb;
+        int leftr, leftg, leftb, lefta;
 
+        put_bits(&s->pb, 8, lefta = data[A]);
         put_bits(&s->pb, 8, leftr = data[R]);
         put_bits(&s->pb, 8, leftg = data[G]);
         put_bits(&s->pb, 8, leftb = data[B]);
-        put_bits(&s->pb, 8, 0);
 
-        sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1, &leftr, &leftg, &leftb);
-        encode_bgr_bitstream(s, width - 1);
+        sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1, &leftr, &leftg, &leftb, &lefta);
+        encode_bgra_bitstream(s, width - 1, 4);
 
         for (y = 1; y < s->height; y++) {
             uint8_t *dst = data + y*stride;
             if (s->predictor == PLANE && s->interlaced < y) {
                 s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
-                sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width, &leftr, &leftg, &leftb);
+                sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width, &leftr, &leftg, &leftb, &lefta);
             } else {
-                sub_left_prediction_bgr32(s, s->temp[0], dst, width, &leftr, &leftg, &leftb);
+                sub_left_prediction_bgr32(s, s->temp[0], dst, width, &leftr, &leftg, &leftb, &lefta);
+            }
+            encode_bgra_bitstream(s, width, 4);
+        }
+    }else if(avctx->pix_fmt == PIX_FMT_RGB24){
+        uint8_t *data = p->data[0] + (height-1)*p->linesize[0];
+        const int stride = -p->linesize[0];
+        const int fake_stride = -fake_ystride;
+        int y;
+        int leftr, leftg, leftb;
+
+        put_bits(&s->pb, 8, leftr= data[0]);
+        put_bits(&s->pb, 8, leftg= data[1]);
+        put_bits(&s->pb, 8, leftb= data[2]);
+        put_bits(&s->pb, 8, 0);
+
+        sub_left_prediction_rgb24(s, s->temp[0], data+3, width-1, &leftr, &leftg, &leftb);
+        encode_bgra_bitstream(s, width-1, 3);
+
+        for(y=1; y<s->height; y++){
+            uint8_t *dst = data + y*stride;
+            if(s->predictor == PLANE && s->interlaced < y){
+                s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width*3);
+                sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width, &leftr, &leftg, &leftb);
+            }else{
+                sub_left_prediction_rgb24(s, s->temp[0], dst, width, &leftr, &leftg, &leftb);
             }
-            encode_bgr_bitstream(s, width);
+            encode_bgra_bitstream(s, width, 3);
         }
     } else {
         av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
@@ -1544,7 +1548,7 @@ AVCodec ff_huffyuv_encoder = {
     .encode2        = encode_frame,
     .close          = encode_end,
     .pix_fmts       = (const enum PixelFormat[]){
-        PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE
+        PIX_FMT_YUV422P, PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_NONE
     },
     .long_name      = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
 };
@@ -1560,7 +1564,7 @@ AVCodec ff_ffvhuff_encoder = {
     .encode2        = encode_frame,
     .close          = encode_end,
     .pix_fmts       = (const enum PixelFormat[]){
-        PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE
+        PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_NONE
     },
     .long_name      = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
 };