2 * This file is part of Libav.
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.
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.
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
22 #include "h264_parse.h"
24 int ff_h264_pred_weight_table(GetBitContext *gb, const SPS *sps,
25 const int *ref_count, int slice_type_nos,
26 H264PredWeightTable *pwt)
29 int luma_def, chroma_def;
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;
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;
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) {
52 pwt->luma_weight_flag[list] = 1;
55 pwt->luma_weight[i][list][0] = luma_def;
56 pwt->luma_weight[i][list][1] = 0;
59 if (sps->chroma_format_idc) {
60 chroma_weight_flag = get_bits1(gb);
61 if (chroma_weight_flag) {
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;
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;
81 if (slice_type_nos != AV_PICTURE_TYPE_B)
84 pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma;
89 * Check if the top & left blocks are available if needed and
90 * change the dc mode so it only uses the available blocks.
92 int ff_h264_check_intra4x4_pred_mode(int8_t *pred_mode_cache, void *logctx,
93 int top_samples_available, int left_samples_available)
95 static const int8_t top[12] = {
96 -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
98 static const int8_t left[12] = {
99 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
103 if (!(top_samples_available & 0x8000)) {
104 for (i = 0; i < 4; i++) {
105 int status = top[pred_mode_cache[scan8[0] + i]];
107 av_log(logctx, AV_LOG_ERROR,
108 "top block unavailable for requested intra4x4 mode %d\n",
110 return AVERROR_INVALIDDATA;
112 pred_mode_cache[scan8[0] + i] = status;
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]];
123 av_log(logctx, AV_LOG_ERROR,
124 "left block unavailable for requested intra4x4 mode %d\n",
126 return AVERROR_INVALIDDATA;
128 pred_mode_cache[scan8[0] + 8 * i] = status;
137 * Check if the top & left blocks are available if needed and
138 * change the dc mode so it only uses the available blocks.
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)
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 };
148 av_log(logctx, AV_LOG_ERROR,
149 "out of range intra chroma pred mode\n");
150 return AVERROR_INVALIDDATA;
153 if (!(top_samples_available & 0x8000)) {
156 av_log(logctx, AV_LOG_ERROR,
157 "top block unavailable for requested intra mode\n");
158 return AVERROR_INVALIDDATA;
162 if ((left_samples_available & 0x8080) != 0x8080) {
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);
171 av_log(logctx, AV_LOG_ERROR,
172 "left block unavailable for requested intra mode\n");
173 return AVERROR_INVALIDDATA;
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)
185 int num_ref_idx_active_override_flag, max_refs;
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];
191 if (slice_type_nos != AV_PICTURE_TYPE_I) {
192 num_ref_idx_active_override_flag = get_bits1(gb);
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;
205 if (slice_type_nos == AV_PICTURE_TYPE_B)
211 ref_count[0] = ref_count[1] = 0;
214 max_refs = picture_structure == PICT_FRAME ? 16 : 32;
216 if (ref_count[0] > max_refs || ref_count[1] > max_refs) {
217 ref_count[0] = ref_count[1] = 0;
218 return AVERROR_INVALIDDATA;
221 *plist_count = list_count;