Browse code

Make decoding alpha optional for some codecs.

For codecs where decoding of a whole plane can simply
be skipped, we should offer applications to not decode
alpha for better performance (ca. 30% less CPU usage
and 40% reduced memory bandwidth).
It also means applications do not need to implement support
(even if it is rather simple) for YUVA formats in order to be
able to play these files.

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>

Reimar Döffinger authored on 2013/08/09 03:27:24
Showing 12 changed files
... ...
@@ -33,6 +33,8 @@ version <next>
33 33
 - SFTP protocol (via libssh)
34 34
 - libx264: add ability to encode in YUVJ422P and YUVJ444P
35 35
 - Fraps: use BT.709 colorspace by default for yuv, as reference fraps decoder does
36
+- make decoding alpha optional for prores, ffv1 and vp6 by setting
37
+  the skip_alpha flag.
36 38
 
37 39
 
38 40
 version 2.0:
... ...
@@ -2865,6 +2865,19 @@ typedef struct AVCodecContext {
2865 2865
 #define FF_SUB_CHARENC_MODE_AUTOMATIC    0  ///< libavcodec will select the mode itself
2866 2866
 #define FF_SUB_CHARENC_MODE_PRE_DECODER  1  ///< the AVPacket data needs to be recoded to UTF-8 before being fed to the decoder, requires iconv
2867 2867
 
2868
+    /**
2869
+     * Skip processing alpha if supported by codec.
2870
+     * Note that if the format uses pre-multiplied alpha (common with VP6,
2871
+     * and recommended due to better video quality/compression)
2872
+     * the image will look as if alpha-blended onto a black background.
2873
+     * However for formats that do not use pre-multiplied alpha
2874
+     * there might be serious artefacts (though e.g. libswscale currently
2875
+     * assumes pre-multiplied alpha anyway).
2876
+     *
2877
+     * - decoding: set by user
2878
+     * - encoding: unused
2879
+     */
2880
+    int skip_alpha;
2868 2881
 } AVCodecContext;
2869 2882
 
2870 2883
 AVRational av_codec_get_pkt_timebase         (const AVCodecContext *avctx);
... ...
@@ -641,6 +641,7 @@ static int read_header(FFV1Context *f)
641 641
     }
642 642
 
643 643
     if (f->colorspace == 0) {
644
+        if (f->avctx->skip_alpha) f->transparency = 0;
644 645
         if (!f->transparency && !f->chroma_planes) {
645 646
             if (f->avctx->bits_per_raw_sample <= 8)
646 647
                 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
... ...
@@ -380,6 +380,7 @@ static const AVOption avcodec_options[] = {
380 380
 {"auto",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_SUB_CHARENC_MODE_AUTOMATIC},   INT_MIN, INT_MAX, S|D, "sub_charenc_mode"},
381 381
 {"pre_decoder", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_SUB_CHARENC_MODE_PRE_DECODER}, INT_MIN, INT_MAX, S|D, "sub_charenc_mode"},
382 382
 {"refcounted_frames", NULL, OFFSET(refcounted_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, A|V|D },
383
+{"skip_alpha", "Skip processing alpha", OFFSET(skip_alpha), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, V|D },
383 384
 {NULL},
384 385
 };
385 386
 
... ...
@@ -97,6 +97,7 @@ static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
97 97
         av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);
98 98
         return AVERROR_INVALIDDATA;
99 99
     }
100
+    if (avctx->skip_alpha) ctx->alpha_info = 0;
100 101
 
101 102
     av_dlog(avctx, "frame type %d\n", ctx->frame_type);
102 103
 
... ...
@@ -140,6 +140,7 @@ static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,
140 140
         av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);
141 141
         return AVERROR_INVALIDDATA;
142 142
     }
143
+    if (avctx->skip_alpha) ctx->alpha_info = 0;
143 144
 
144 145
     switch (ctx->chroma_factor) {
145 146
     case 2:
... ...
@@ -609,7 +610,7 @@ static int decode_slice(AVCodecContext *avctx, void *tdata)
609 609
     coff[2]     = coff[1] + u_data_size;
610 610
     v_data_size = hdr_size > 7 ? AV_RB16(buf + 6) : slice_data_size - coff[2];
611 611
     coff[3]     = coff[2] + v_data_size;
612
-    a_data_size = slice_data_size - coff[3];
612
+    a_data_size = ctx->alpha_info ? slice_data_size - coff[3] : 0;
613 613
 
614 614
     /* if V or alpha component size is negative that means that previous
615 615
        component sizes are too large */
... ...
@@ -530,7 +530,7 @@ int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
530 530
     if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0)
531 531
         return -1;
532 532
 
533
-    if (s->has_alpha) {
533
+    if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
534 534
         av_frame_unref(s->alpha_context->frames[VP56_FRAME_CURRENT]);
535 535
         if ((ret = av_frame_ref(s->alpha_context->frames[VP56_FRAME_CURRENT], p)) < 0) {
536 536
             av_frame_unref(p);
... ...
@@ -545,7 +545,7 @@ int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
545 545
         }
546 546
     }
547 547
 
548
-    if (s->has_alpha) {
548
+    if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) {
549 549
         int bak_w = avctx->width;
550 550
         int bak_h = avctx->height;
551 551
         int bak_cw = avctx->coded_width;
... ...
@@ -567,7 +567,7 @@ int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
567 567
         }
568 568
     }
569 569
 
570
-    avctx->execute2(avctx, ff_vp56_decode_mbs, 0, 0, s->has_alpha + 1);
570
+    avctx->execute2(avctx, ff_vp56_decode_mbs, 0, 0, (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) + 1);
571 571
 
572 572
     if ((res = av_frame_ref(data, p)) < 0)
573 573
         return res;
... ...
@@ -690,6 +690,7 @@ av_cold int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
690 690
 
691 691
     s->avctx = avctx;
692 692
     avctx->pix_fmt = has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
693
+    if (avctx->skip_alpha) avctx->pix_fmt = AV_PIX_FMT_YUV420P;
693 694
 
694 695
     ff_h264chroma_init(&s->h264chroma, 8);
695 696
     ff_hpeldsp_init(&s->hdsp, avctx->flags);
... ...
@@ -3,7 +3,9 @@ FATE_PRORES = fate-prores-422                                           \
3 3
               fate-prores-422_lt                                        \
4 4
               fate-prores-422_proxy                                     \
5 5
               fate-prores-alpha                                         \
6
+              fate-prores-alpha_skip                                    \
6 7
               fate-prores-transparency                                  \
8
+              fate-prores-transparency_skip                             \
7 9
 
8 10
 FATE_SAMPLES_AVCONV-$(call DEMDEC, MOV, PRORES) += $(FATE_PRORES)
9 11
 fate-prores: $(FATE_PRORES)
... ...
@@ -13,4 +15,6 @@ fate-prores-422_hq:    CMD = framecrc -flags +bitexact -i $(TARGET_SAMPLES)/pror
13 13
 fate-prores-422_lt:    CMD = framecrc -flags +bitexact -i $(TARGET_SAMPLES)/prores/Sequence_1-Apple_ProRes_422_LT.mov -pix_fmt yuv422p10le
14 14
 fate-prores-422_proxy: CMD = framecrc -flags +bitexact -i $(TARGET_SAMPLES)/prores/Sequence_1-Apple_ProRes_422_Proxy.mov -pix_fmt yuv422p10le
15 15
 fate-prores-alpha:     CMD = framecrc -flags +bitexact -i $(TARGET_SAMPLES)/prores/Sequence_1-Apple_ProRes_with_Alpha.mov -pix_fmt yuva444p10le
16
+fate-prores-alpha_skip: CMD = framecrc -flags +bitexact -skip_alpha 1 -i $(TARGET_SAMPLES)/prores/Sequence_1-Apple_ProRes_with_Alpha.mov
16 17
 fate-prores-transparency: CMD = framecrc -flags +bitexact -i $(TARGET_SAMPLES)/prores/prores4444_with_transparency.mov -pix_fmt yuva444p10le
18
+fate-prores-transparency_skip: CMD = framecrc -flags +bitexact -skip_alpha 1 -i $(TARGET_SAMPLES)/prores/prores4444_with_transparency.mov
... ...
@@ -19,6 +19,9 @@ fate-vp61: CMD = framecrc -flags +bitexact -i $(TARGET_SAMPLES)/ea-vp6/MovieSkir
19 19
 FATE_VP6-$(call DEMDEC, FLV, VP6A) += fate-vp6a
20 20
 fate-vp6a: CMD = framecrc -flags +bitexact -i $(TARGET_SAMPLES)/flash-vp6/300x180-Scr-f8-056alpha.flv
21 21
 
22
+FATE_VP6-$(call DEMDEC, FLV, VP6A) += fate-vp6a-skip_alpha
23
+fate-vp6a-skip_alpha: CMD = framecrc -flags +bitexact -skip_alpha 1 -i $(TARGET_SAMPLES)/flash-vp6/300x180-Scr-f8-056alpha.flv
24
+
22 25
 FATE_VP6-$(call DEMDEC, FLV, VP6F) += fate-vp6f
23 26
 fate-vp6f: CMD = framecrc -flags +bitexact -i $(TARGET_SAMPLES)/flash-vp6/clip1024.flv
24 27
 
25 28
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+#tb 0: 100/2997
1
+0,          0,          0,        1, 12441600, 0x254d8f95
2
+0,          1,          1,        1, 12441600, 0x254d8f95
0 3
new file mode 100644
... ...
@@ -0,0 +1,5 @@
0
+#tb 0: 1/25
1
+#tb 1: 1/48000
2
+0,          0,          0,        1, 12441600, 0x58bff47d
3
+1,          0,          0,     1024,     4096, 0x00000000
4
+1,       1024,       1024,      896,     3584, 0x00000000
0 5
new file mode 100644
... ...
@@ -0,0 +1,94 @@
0
+#tb 0: 1/4
1
+0,          0,          0,        1,    81000, 0xcb92962d
2
+0,          1,          1,        1,    81000, 0xae381904
3
+0,          2,          2,        1,    81000, 0x1fcc0c75
4
+0,          3,          3,        1,    81000, 0x023f0c21
5
+0,          4,          4,        1,    81000, 0xad691402
6
+0,          5,          5,        1,    81000, 0x42390be0
7
+0,          6,          6,        1,    81000, 0xc1c10a4e
8
+0,          7,          7,        1,    81000, 0x9c0315ac
9
+0,          8,          8,        1,    81000, 0xc2a315a7
10
+0,          9,          9,        1,    81000, 0x3a631392
11
+0,         10,         10,        1,    81000, 0x11591414
12
+0,         11,         11,        1,    81000, 0x1a551125
13
+0,         12,         12,        1,    81000, 0x2e1efa4f
14
+0,         13,         13,        1,    81000, 0x4aa3f016
15
+0,         14,         14,        1,    81000, 0x74c029d8
16
+0,         15,         15,        1,    81000, 0xdee9a98b
17
+0,         16,         16,        1,    81000, 0xdf3502d5
18
+0,         17,         17,        1,    81000, 0x4653536b
19
+0,         18,         18,        1,    81000, 0x7f658c75
20
+0,         19,         19,        1,    81000, 0xab18ff13
21
+0,         20,         20,        1,    81000, 0xac2b8f3b
22
+0,         21,         21,        1,    81000, 0xd61ff094
23
+0,         22,         22,        1,    81000, 0x425bfc2b
24
+0,         23,         23,        1,    81000, 0x6be7ecd3
25
+0,         24,         24,        1,    81000, 0x0b0ee65b
26
+0,         25,         25,        1,    81000, 0x3c6f146b
27
+0,         26,         26,        1,    81000, 0x27c4e9c8
28
+0,         27,         27,        1,    81000, 0x174022c4
29
+0,         28,         28,        1,    81000, 0x3320fe81
30
+0,         29,         29,        1,    81000, 0x7a3c342e
31
+0,         30,         30,        1,    81000, 0x448b4346
32
+0,         31,         31,        1,    81000, 0xd285b23d
33
+0,         32,         32,        1,    81000, 0x852ed590
34
+0,         33,         33,        1,    81000, 0xc9d3df17
35
+0,         34,         34,        1,    81000, 0x4d23727b
36
+0,         35,         35,        1,    81000, 0x1fae66cd
37
+0,         36,         36,        1,    81000, 0x384d54ab
38
+0,         37,         37,        1,    81000, 0x2fee6ba3
39
+0,         38,         38,        1,    81000, 0xd7ad6f59
40
+0,         39,         39,        1,    81000, 0xaf5e3e76
41
+0,         40,         40,        1,    81000, 0x10fceda4
42
+0,         41,         41,        1,    81000, 0xb26df92b
43
+0,         42,         42,        1,    81000, 0xd6676e08
44
+0,         43,         43,        1,    81000, 0xff6b1b95
45
+0,         44,         44,        1,    81000, 0x6196d598
46
+0,         45,         45,        1,    81000, 0x833ebf1b
47
+0,         46,         46,        1,    81000, 0x7b085af1
48
+0,         47,         47,        1,    81000, 0xe8f583b4
49
+0,         48,         48,        1,    81000, 0x3426d5e4
50
+0,         49,         49,        1,    81000, 0x214069ed
51
+0,         50,         50,        1,    81000, 0x7dbdfd3f
52
+0,         51,         51,        1,    81000, 0xf19b3f45
53
+0,         52,         52,        1,    81000, 0x0f05c7e2
54
+0,         53,         53,        1,    81000, 0xba94e323
55
+0,         54,         54,        1,    81000, 0x0de7b0c2
56
+0,         55,         55,        1,    81000, 0xfcf93c55
57
+0,         56,         56,        1,    81000, 0x8a8dbd55
58
+0,         57,         57,        1,    81000, 0xddf22b97
59
+0,         58,         58,        1,    81000, 0x49a830ff
60
+0,         59,         59,        1,    81000, 0x82ab2a4b
61
+0,         60,         60,        1,    81000, 0xd23420e5
62
+0,         61,         61,        1,    81000, 0x7c1017d1
63
+0,         62,         62,        1,    81000, 0x9aa61b38
64
+0,         63,         63,        1,    81000, 0x2a724a18
65
+0,         64,         64,        1,    81000, 0xc18055f2
66
+0,         65,         65,        1,    81000, 0xecba3855
67
+0,         66,         66,        1,    81000, 0x0eed6b0f
68
+0,         67,         67,        1,    81000, 0x4be73816
69
+0,         68,         68,        1,    81000, 0xa681214e
70
+0,         69,         69,        1,    81000, 0x4958f83d
71
+0,         70,         70,        1,    81000, 0xca0f0d61
72
+0,         71,         71,        1,    81000, 0x3c453de1
73
+0,         72,         72,        1,    81000, 0xff60360a
74
+0,         73,         73,        1,    81000, 0xdcef0949
75
+0,         74,         74,        1,    81000, 0xe5e3732d
76
+0,         75,         75,        1,    81000, 0x39747fd4
77
+0,         76,         76,        1,    81000, 0x6bec70e6
78
+0,         77,         77,        1,    81000, 0x7026a8c0
79
+0,         78,         78,        1,    81000, 0x92de5b61
80
+0,         79,         79,        1,    81000, 0x3f00507f
81
+0,         80,         80,        1,    81000, 0x5620c377
82
+0,         81,         81,        1,    81000, 0x39f5ed38
83
+0,         82,         82,        1,    81000, 0x6ee35d67
84
+0,         83,         83,        1,    81000, 0x4f99a409
85
+0,         84,         84,        1,    81000, 0x0a05b6ea
86
+0,         85,         85,        1,    81000, 0xd6c442d9
87
+0,         86,         86,        1,    81000, 0x0bb3d2f0
88
+0,         87,         87,        1,    81000, 0x6891c5b1
89
+0,         88,         88,        1,    81000, 0xf16ba9be
90
+0,         89,         89,        1,    81000, 0xba53528e
91
+0,         90,         90,        1,    81000, 0xc847de49
92
+0,         91,         91,        1,    81000, 0xc5b2e2b0
93
+0,         92,         92,        1,    81000, 0xb0b497ff