]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ffv1enc.c
Merge commit '582d4211e00015b68626f77ce4af53161e2b1713'
[ffmpeg] / libavcodec / ffv1enc.c
index 948a23041940d6b2dc4d23a2ff913508f6645c0b..68d311d1b87944ac53f2ab8d9cd73af93f9b0396 100644 (file)
@@ -268,107 +268,15 @@ static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
     update_vlc_state(state, v);
 }
 
-static av_always_inline int encode_line(FFV1Context *s, int w,
-                                        int16_t *sample[3],
-                                        int plane_index, int bits)
-{
-    PlaneContext *const p = &s->plane[plane_index];
-    RangeCoder *const c   = &s->c;
-    int x;
-    int run_index = s->run_index;
-    int run_count = 0;
-    int run_mode  = 0;
-
-    if (s->ac != AC_GOLOMB_RICE) {
-        if (c->bytestream_end - c->bytestream < w * 35) {
-            av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
-            return AVERROR_INVALIDDATA;
-        }
-    } else {
-        if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
-            av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
-            return AVERROR_INVALIDDATA;
-        }
-    }
+#define TYPE int16_t
+#define RENAME(name) name
+#include "ffv1enc_template.c"
+#undef TYPE
+#undef RENAME
 
-    if (s->slice_coding_mode == 1) {
-        for (x = 0; x < w; x++) {
-            int i;
-            int v = sample[0][x];
-            for (i = bits-1; i>=0; i--) {
-                uint8_t state = 128;
-                put_rac(c, &state, (v>>i) & 1);
-            }
-        }
-        return 0;
-    }
-
-    for (x = 0; x < w; x++) {
-        int diff, context;
-
-        context = get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
-        diff    = sample[0][x] - predict(sample[0] + x, sample[1] + x);
-
-        if (context < 0) {
-            context = -context;
-            diff    = -diff;
-        }
-
-        diff = fold(diff, bits);
-
-        if (s->ac != AC_GOLOMB_RICE) {
-            if (s->flags & AV_CODEC_FLAG_PASS1) {
-                put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
-                                  s->rc_stat2[p->quant_table_index][context]);
-            } else {
-                put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
-            }
-        } else {
-            if (context == 0)
-                run_mode = 1;
-
-            if (run_mode) {
-                if (diff) {
-                    while (run_count >= 1 << ff_log2_run[run_index]) {
-                        run_count -= 1 << ff_log2_run[run_index];
-                        run_index++;
-                        put_bits(&s->pb, 1, 1);
-                    }
-
-                    put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
-                    if (run_index)
-                        run_index--;
-                    run_count = 0;
-                    run_mode  = 0;
-                    if (diff > 0)
-                        diff--;
-                } else {
-                    run_count++;
-                }
-            }
-
-            ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
-                    run_count, run_index, run_mode, x,
-                    (int)put_bits_count(&s->pb));
-
-            if (run_mode == 0)
-                put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
-        }
-    }
-    if (run_mode) {
-        while (run_count >= 1 << ff_log2_run[run_index]) {
-            run_count -= 1 << ff_log2_run[run_index];
-            run_index++;
-            put_bits(&s->pb, 1, 1);
-        }
-
-        if (run_count)
-            put_bits(&s->pb, 1, 1);
-    }
-    s->run_index = run_index;
-
-    return 0;
-}
+#define TYPE int32_t
+#define RENAME(name) name ## 32
+#include "ffv1enc_template.c"
 
 static int encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
                          int stride, int plane_index, int pixel_stride)
@@ -410,68 +318,6 @@ static int encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
     return 0;
 }
 
-static int encode_rgb_frame(FFV1Context *s, const uint8_t *src[3],
-                             int w, int h, const int stride[3])
-{
-    int x, y, p, i;
-    const int ring_size = s->context_model ? 3 : 2;
-    int16_t *sample[4][3];
-    int lbd    = s->bits_per_raw_sample <= 8;
-    int bits   = s->bits_per_raw_sample > 0 ? s->bits_per_raw_sample : 8;
-    int offset = 1 << bits;
-
-    s->run_index = 0;
-
-    memset(s->sample_buffer, 0, ring_size * MAX_PLANES *
-                                (w + 6) * sizeof(*s->sample_buffer));
-
-    for (y = 0; y < h; y++) {
-        for (i = 0; i < ring_size; i++)
-            for (p = 0; p < MAX_PLANES; p++)
-                sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
-
-        for (x = 0; x < w; x++) {
-            int b, g, r, av_uninit(a);
-            if (lbd) {
-                unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
-                b =  v        & 0xFF;
-                g = (v >>  8) & 0xFF;
-                r = (v >> 16) & 0xFF;
-                a =  v >> 24;
-            } else {
-                b = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
-                g = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
-                r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
-            }
-
-            if (s->slice_coding_mode != 1) {
-                b -= g;
-                r -= g;
-                g += (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
-                b += offset;
-                r += offset;
-            }
-
-            sample[0][0][x] = g;
-            sample[1][0][x] = b;
-            sample[2][0][x] = r;
-            sample[3][0][x] = a;
-        }
-        for (p = 0; p < 3 + s->transparency; p++) {
-            int ret;
-            sample[p][0][-1] = sample[p][1][0  ];
-            sample[p][1][ w] = sample[p][1][w-1];
-            if (lbd && s->slice_coding_mode == 0)
-                ret = encode_line(s, w, sample[p], (p + 1) / 2, 9);
-            else
-                ret = encode_line(s, w, sample[p], (p + 1) / 2, bits + (s->slice_coding_mode != 1));
-            if (ret < 0)
-                return ret;
-        }
-    }
-    return 0;
-}
-
 static void write_quant_table(RangeCoder *c, int16_t *quant_table)
 {
     int last = 0;
@@ -752,11 +598,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
             av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
             return AVERROR_INVALIDDATA;
         }
-        if (s->ac == AC_GOLOMB_RICE) {
-            av_log(avctx, AV_LOG_INFO,
-                   "bits_per_raw_sample > 8, forcing range coder\n");
-            s->ac = AC_RANGE_CUSTOM_TAB;
-        }
         s->version = FFMAX(s->version, 1);
     case AV_PIX_FMT_GRAY8:
     case AV_PIX_FMT_YA8:
@@ -781,14 +622,23 @@ FF_ENABLE_DEPRECATION_WARNINGS
         s->colorspace = 1;
         s->transparency = 1;
         s->chroma_planes = 1;
-        if (!avctx->bits_per_raw_sample)
-            s->bits_per_raw_sample = 8;
+        s->bits_per_raw_sample = 8;
+        break;
+    case AV_PIX_FMT_RGB48:
+        s->colorspace = 1;
+        s->chroma_planes = 1;
+        s->bits_per_raw_sample = 16;
+        s->use32bit = 1;
+        s->version = FFMAX(s->version, 1);
+        if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
+            av_log(avctx, AV_LOG_ERROR, "16bit RGB is experimental and under development, only use it for experiments\n");
+            return AVERROR_INVALIDDATA;
+        }
         break;
     case AV_PIX_FMT_0RGB32:
         s->colorspace = 1;
         s->chroma_planes = 1;
-        if (!avctx->bits_per_raw_sample)
-            s->bits_per_raw_sample = 8;
+        s->bits_per_raw_sample = 8;
         break;
     case AV_PIX_FMT_GBRP9:
         if (!avctx->bits_per_raw_sample)
@@ -802,16 +652,21 @@ FF_ENABLE_DEPRECATION_WARNINGS
     case AV_PIX_FMT_GBRP14:
         if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
             s->bits_per_raw_sample = 14;
+    case AV_PIX_FMT_GBRP16:
+        if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
+            s->bits_per_raw_sample = 16;
         else if (!s->bits_per_raw_sample)
             s->bits_per_raw_sample = avctx->bits_per_raw_sample;
         s->colorspace = 1;
         s->chroma_planes = 1;
-        s->version = FFMAX(s->version, 1);
-        if (s->ac == AC_GOLOMB_RICE) {
-            av_log(avctx, AV_LOG_INFO,
-                   "bits_per_raw_sample > 8, forcing coder 1\n");
-            s->ac = AC_RANGE_CUSTOM_TAB;
+        if (s->bits_per_raw_sample >= 16) {
+            s->use32bit = 1;
+            if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
+                av_log(avctx, AV_LOG_ERROR, "16bit RGB is experimental and under development, only use it for experiments\n");
+                return AVERROR_INVALIDDATA;
+            }
         }
+        s->version = FFMAX(s->version, 1);
         break;
     default:
         av_log(avctx, AV_LOG_ERROR, "format not supported\n");
@@ -819,6 +674,13 @@ FF_ENABLE_DEPRECATION_WARNINGS
     }
     av_assert0(s->bits_per_raw_sample >= 8);
 
+    if (s->bits_per_raw_sample > 8) {
+        if (s->ac == AC_GOLOMB_RICE) {
+            av_log(avctx, AV_LOG_INFO,
+                    "bits_per_raw_sample > 8, forcing range coder\n");
+            s->ac = AC_RANGE_CUSTOM_TAB;
+        }
+    }
     if (s->transparency) {
         av_log(avctx, AV_LOG_WARNING, "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
     }
@@ -1156,8 +1018,8 @@ static int encode_slice(AVCodecContext *c, void *arg)
     int ret;
     RangeCoder c_bak = fs->c;
     const uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
-                                p->data[1] + ps*x + y*p->linesize[1],
-                                p->data[2] + ps*x + y*p->linesize[2]};
+                                p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
+                                p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL};
 
     fs->slice_coding_mode = 0;
     if (f->version > 3) {
@@ -1199,6 +1061,8 @@ retry:
     } else if (c->pix_fmt == AV_PIX_FMT_YA8) {
         ret  = encode_plane(fs, p->data[0] +     ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
         ret |= encode_plane(fs, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
+    } else if (f->use32bit) {
+        ret = encode_rgb_frame32(fs, planes, width, height, p->linesize);
     } else {
         ret = encode_rgb_frame(fs, planes, width, height, p->linesize);
     }
@@ -1230,7 +1094,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     uint8_t *buf_p;
     int i, ret;
     int64_t maxsize =   AV_INPUT_BUFFER_MIN_SIZE
-                      + avctx->width*avctx->height*35LL*4;
+                      + avctx->width*avctx->height*37LL*4;
 
     if(!pict) {
         if (avctx->flags & AV_CODEC_FLAG_PASS1) {
@@ -1434,6 +1298,7 @@ AVCodec ff_ffv1_encoder = {
         AV_PIX_FMT_GRAY16,    AV_PIX_FMT_GRAY8,     AV_PIX_FMT_GBRP9,     AV_PIX_FMT_GBRP10,
         AV_PIX_FMT_GBRP12,    AV_PIX_FMT_GBRP14,
         AV_PIX_FMT_YA8,
+        AV_PIX_FMT_GBRP16, AV_PIX_FMT_RGB48,
         AV_PIX_FMT_NONE
 
     },