]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_parse.c
h264: factor out parsing the reference count into a separate file
[ffmpeg] / libavcodec / h264_parse.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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     luma_def   = 1 << pwt->luma_log2_weight_denom;
37     chroma_def = 1 << pwt->chroma_log2_weight_denom;
38
39     for (list = 0; list < 2; list++) {
40         pwt->luma_weight_flag[list]   = 0;
41         pwt->chroma_weight_flag[list] = 0;
42         for (i = 0; i < ref_count[list]; i++) {
43             int luma_weight_flag, chroma_weight_flag;
44
45             luma_weight_flag = get_bits1(gb);
46             if (luma_weight_flag) {
47                 pwt->luma_weight[i][list][0] = get_se_golomb(gb);
48                 pwt->luma_weight[i][list][1] = get_se_golomb(gb);
49                 if (pwt->luma_weight[i][list][0] != luma_def ||
50                     pwt->luma_weight[i][list][1] != 0) {
51                     pwt->use_weight             = 1;
52                     pwt->luma_weight_flag[list] = 1;
53                 }
54             } else {
55                 pwt->luma_weight[i][list][0] = luma_def;
56                 pwt->luma_weight[i][list][1] = 0;
57             }
58
59             if (sps->chroma_format_idc) {
60                 chroma_weight_flag = get_bits1(gb);
61                 if (chroma_weight_flag) {
62                     int j;
63                     for (j = 0; j < 2; j++) {
64                         pwt->chroma_weight[i][list][j][0] = get_se_golomb(gb);
65                         pwt->chroma_weight[i][list][j][1] = get_se_golomb(gb);
66                         if (pwt->chroma_weight[i][list][j][0] != chroma_def ||
67                             pwt->chroma_weight[i][list][j][1] != 0) {
68                             pwt->use_weight_chroma        = 1;
69                             pwt->chroma_weight_flag[list] = 1;
70                         }
71                     }
72                 } else {
73                     int j;
74                     for (j = 0; j < 2; j++) {
75                         pwt->chroma_weight[i][list][j][0] = chroma_def;
76                         pwt->chroma_weight[i][list][j][1] = 0;
77                     }
78                 }
79             }
80         }
81         if (slice_type_nos != AV_PICTURE_TYPE_B)
82             break;
83     }
84     pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma;
85     return 0;
86 }
87
88 /**
89  * Check if the top & left blocks are available if needed and
90  * change the dc mode so it only uses the available blocks.
91  */
92 int ff_h264_check_intra4x4_pred_mode(int8_t *pred_mode_cache, void *logctx,
93                                      int top_samples_available, int left_samples_available)
94 {
95     static const int8_t top[12] = {
96         -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
97     };
98     static const int8_t left[12] = {
99         0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
100     };
101     int i;
102
103     if (!(top_samples_available & 0x8000)) {
104         for (i = 0; i < 4; i++) {
105             int status = top[pred_mode_cache[scan8[0] + i]];
106             if (status < 0) {
107                 av_log(logctx, AV_LOG_ERROR,
108                        "top block unavailable for requested intra4x4 mode %d\n",
109                        status);
110                 return AVERROR_INVALIDDATA;
111             } else if (status) {
112                 pred_mode_cache[scan8[0] + i] = status;
113             }
114         }
115     }
116
117     if ((left_samples_available & 0x8888) != 0x8888) {
118         static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
119         for (i = 0; i < 4; i++)
120             if (!(left_samples_available & mask[i])) {
121                 int status = left[pred_mode_cache[scan8[0] + 8 * i]];
122                 if (status < 0) {
123                     av_log(logctx, AV_LOG_ERROR,
124                            "left block unavailable for requested intra4x4 mode %d\n",
125                            status);
126                     return AVERROR_INVALIDDATA;
127                 } else if (status) {
128                     pred_mode_cache[scan8[0] + 8 * i] = status;
129                 }
130             }
131     }
132
133     return 0;
134 }
135
136 /**
137  * Check if the top & left blocks are available if needed and
138  * change the dc mode so it only uses the available blocks.
139  */
140 int ff_h264_check_intra_pred_mode(void *logctx, int top_samples_available,
141                                   int left_samples_available,
142                                   int mode, int is_chroma)
143 {
144     static const int8_t top[4]  = { LEFT_DC_PRED8x8, 1, -1, -1 };
145     static const int8_t left[5] = { TOP_DC_PRED8x8, -1,  2, -1, DC_128_PRED8x8 };
146
147     if (mode > 3U) {
148         av_log(logctx, AV_LOG_ERROR,
149                "out of range intra chroma pred mode\n");
150         return AVERROR_INVALIDDATA;
151     }
152
153     if (!(top_samples_available & 0x8000)) {
154         mode = top[mode];
155         if (mode < 0) {
156             av_log(logctx, AV_LOG_ERROR,
157                    "top block unavailable for requested intra mode\n");
158             return AVERROR_INVALIDDATA;
159         }
160     }
161
162     if ((left_samples_available & 0x8080) != 0x8080) {
163         mode = left[mode];
164         if (is_chroma && (left_samples_available & 0x8080)) {
165             // mad cow disease mode, aka MBAFF + constrained_intra_pred
166             mode = ALZHEIMER_DC_L0T_PRED8x8 +
167                    (!(left_samples_available & 0x8000)) +
168                    2 * (mode == DC_128_PRED8x8);
169         }
170         if (mode < 0) {
171             av_log(logctx, AV_LOG_ERROR,
172                    "left block unavailable for requested intra mode\n");
173             return AVERROR_INVALIDDATA;
174         }
175     }
176
177     return mode;
178 }
179
180 int ff_h264_parse_ref_count(int *plist_count, int ref_count[2],
181                             GetBitContext *gb, const PPS *pps,
182                             int slice_type_nos, int picture_structure)
183 {
184     int list_count;
185     int num_ref_idx_active_override_flag, max_refs;
186
187     // set defaults, might be overridden a few lines later
188     ref_count[0] = pps->ref_count[0];
189     ref_count[1] = pps->ref_count[1];
190
191     if (slice_type_nos != AV_PICTURE_TYPE_I) {
192         num_ref_idx_active_override_flag = get_bits1(gb);
193
194         if (num_ref_idx_active_override_flag) {
195             ref_count[0] = get_ue_golomb(gb) + 1;
196             if (ref_count[0] < 1)
197                 return AVERROR_INVALIDDATA;
198             if (slice_type_nos == AV_PICTURE_TYPE_B) {
199                 ref_count[1] = get_ue_golomb(gb) + 1;
200                 if (ref_count[1] < 1)
201                     return AVERROR_INVALIDDATA;
202             }
203         }
204
205         if (slice_type_nos == AV_PICTURE_TYPE_B)
206             list_count = 2;
207         else
208             list_count = 1;
209     } else {
210         list_count   = 0;
211         ref_count[0] = ref_count[1] = 0;
212     }
213
214     max_refs = picture_structure == PICT_FRAME ? 16 : 32;
215
216     if (ref_count[0] > max_refs || ref_count[1] > max_refs) {
217         ref_count[0] = ref_count[1] = 0;
218         return AVERROR_INVALIDDATA;
219     }
220
221     *plist_count = list_count;
222
223     return 0;
224 }