]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_parse.c
Merge commit 'b8e899f4bf5f09900aa71552112d32a5566b6baf'
[ffmpeg] / libavcodec / h264_parse.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "get_bits.h"
20 #include "golomb.h"
21 #include "h264.h"
22 #include "h264_parse.h"
23
24 int ff_h264_pred_weight_table(GetBitContext *gb, const SPS *sps,
25                               const int *ref_count, int slice_type_nos,
26                               H264PredWeightTable *pwt)
27 {
28     int list, i;
29     int luma_def, chroma_def;
30
31     pwt->use_weight             = 0;
32     pwt->use_weight_chroma      = 0;
33     pwt->luma_log2_weight_denom = get_ue_golomb(gb);
34     if (sps->chroma_format_idc)
35         pwt->chroma_log2_weight_denom = get_ue_golomb(gb);
36
37     if (pwt->luma_log2_weight_denom > 7U) {
38         av_log(NULL, AV_LOG_ERROR, "luma_log2_weight_denom %d is out of range\n", pwt->luma_log2_weight_denom);
39         pwt->luma_log2_weight_denom = 0;
40     }
41     if (pwt->chroma_log2_weight_denom > 7U) {
42         av_log(NULL, AV_LOG_ERROR, "chroma_log2_weight_denom %d is out of range\n", pwt->chroma_log2_weight_denom);
43         pwt->chroma_log2_weight_denom = 0;
44     }
45
46     luma_def   = 1 << pwt->luma_log2_weight_denom;
47     chroma_def = 1 << pwt->chroma_log2_weight_denom;
48
49     for (list = 0; list < 2; list++) {
50         pwt->luma_weight_flag[list]   = 0;
51         pwt->chroma_weight_flag[list] = 0;
52         for (i = 0; i < ref_count[list]; i++) {
53             int luma_weight_flag, chroma_weight_flag;
54
55             luma_weight_flag = get_bits1(gb);
56             if (luma_weight_flag) {
57                 pwt->luma_weight[i][list][0] = get_se_golomb(gb);
58                 pwt->luma_weight[i][list][1] = get_se_golomb(gb);
59                 if (pwt->luma_weight[i][list][0] != luma_def ||
60                     pwt->luma_weight[i][list][1] != 0) {
61                     pwt->use_weight             = 1;
62                     pwt->luma_weight_flag[list] = 1;
63                 }
64             } else {
65                 pwt->luma_weight[i][list][0] = luma_def;
66                 pwt->luma_weight[i][list][1] = 0;
67             }
68
69             if (sps->chroma_format_idc) {
70                 chroma_weight_flag = get_bits1(gb);
71                 if (chroma_weight_flag) {
72                     int j;
73                     for (j = 0; j < 2; j++) {
74                         pwt->chroma_weight[i][list][j][0] = get_se_golomb(gb);
75                         pwt->chroma_weight[i][list][j][1] = get_se_golomb(gb);
76                         if (pwt->chroma_weight[i][list][j][0] != chroma_def ||
77                             pwt->chroma_weight[i][list][j][1] != 0) {
78                             pwt->use_weight_chroma        = 1;
79                             pwt->chroma_weight_flag[list] = 1;
80                         }
81                     }
82                 } else {
83                     int j;
84                     for (j = 0; j < 2; j++) {
85                         pwt->chroma_weight[i][list][j][0] = chroma_def;
86                         pwt->chroma_weight[i][list][j][1] = 0;
87                     }
88                 }
89             }
90         }
91         if (slice_type_nos != AV_PICTURE_TYPE_B)
92             break;
93     }
94     pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma;
95     return 0;
96 }
97
98 /**
99  * Check if the top & left blocks are available if needed and
100  * change the dc mode so it only uses the available blocks.
101  */
102 int ff_h264_check_intra4x4_pred_mode(int8_t *pred_mode_cache, void *logctx,
103                                      int top_samples_available, int left_samples_available)
104 {
105     static const int8_t top[12] = {
106         -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
107     };
108     static const int8_t left[12] = {
109         0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
110     };
111     int i;
112
113     if (!(top_samples_available & 0x8000)) {
114         for (i = 0; i < 4; i++) {
115             int status = top[pred_mode_cache[scan8[0] + i]];
116             if (status < 0) {
117                 av_log(logctx, AV_LOG_ERROR,
118                                            "top block unavailable for requested intra mode %d\n",
119                        status);
120                 return AVERROR_INVALIDDATA;
121             } else if (status) {
122                 pred_mode_cache[scan8[0] + i] = status;
123             }
124         }
125     }
126
127     if ((left_samples_available & 0x8888) != 0x8888) {
128         static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
129         for (i = 0; i < 4; i++)
130             if (!(left_samples_available & mask[i])) {
131                 int status = left[pred_mode_cache[scan8[0] + 8 * i]];
132                 if (status < 0) {
133                     av_log(logctx, AV_LOG_ERROR,
134                            "left block unavailable for requested intra4x4 mode %d\n",
135                            status);
136                     return AVERROR_INVALIDDATA;
137                 } else if (status) {
138                     pred_mode_cache[scan8[0] + 8 * i] = status;
139                 }
140             }
141     }
142
143     return 0;
144 }
145
146 /**
147  * Check if the top & left blocks are available if needed and
148  * change the dc mode so it only uses the available blocks.
149  */
150 int ff_h264_check_intra_pred_mode(void *logctx, int top_samples_available,
151                                   int left_samples_available,
152                                   int mode, int is_chroma)
153 {
154     static const int8_t top[4]  = { LEFT_DC_PRED8x8, 1, -1, -1 };
155     static const int8_t left[5] = { TOP_DC_PRED8x8, -1,  2, -1, DC_128_PRED8x8 };
156
157     if (mode > 3U) {
158         av_log(logctx, AV_LOG_ERROR,
159                "out of range intra chroma pred mode\n");
160         return AVERROR_INVALIDDATA;
161     }
162
163     if (!(top_samples_available & 0x8000)) {
164         mode = top[mode];
165         if (mode < 0) {
166             av_log(logctx, AV_LOG_ERROR,
167                    "top block unavailable for requested intra mode\n");
168             return AVERROR_INVALIDDATA;
169         }
170     }
171
172     if ((left_samples_available & 0x8080) != 0x8080) {
173         mode = left[mode];
174         if (mode < 0) {
175             av_log(logctx, AV_LOG_ERROR,
176                    "left block unavailable for requested intra mode\n");
177             return AVERROR_INVALIDDATA;
178         }
179         if (is_chroma && (left_samples_available & 0x8080)) {
180             // mad cow disease mode, aka MBAFF + constrained_intra_pred
181             mode = ALZHEIMER_DC_L0T_PRED8x8 +
182                    (!(left_samples_available & 0x8000)) +
183                    2 * (mode == DC_128_PRED8x8);
184         }
185     }
186
187     return mode;
188 }