Merge pull request #218 from saper/build-fixes
[envytools.git] / vstream / h264_slice.c
blob196d153ec6ebc62af586144df67d4c115a52ed43
1 /*
2 * Copyright (C) 2011 Marcelina Koƛcielnicka <mwk@0x04.net>
3 * All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 #include "h264.h"
26 #include "h264_cabac.h"
27 #include "util.h"
28 #include <stdio.h>
29 #include <stdlib.h>
31 int h264_mb_slice_group(struct h264_slice *slice, uint32_t mbaddr) {
32 if (mbaddr >= slice->pic_size_in_mbs)
33 return 0;
34 if (!slice->picparm->num_slice_groups_minus1)
35 return 0;
36 int ret;
37 if (slice->seqparm->frame_mbs_only_flag || slice->field_pic_flag) {
38 ret = slice->sgmap[mbaddr];
39 } else if (!slice->mbaff_frame_flag) {
40 ret = slice->sgmap[mbaddr/2];
41 } else {
42 int x = mbaddr % slice->pic_width_in_mbs;
43 int y = mbaddr / slice->pic_width_in_mbs;
44 ret = slice->sgmap[y / 2 * slice->pic_width_in_mbs + x];
46 return ret;
49 int h264_mb_avail(struct h264_slice *slice, uint32_t mbaddr) {
50 if (mbaddr < slice->first_mb_in_slice * (slice->mbaff_frame_flag + 1)
51 || mbaddr > slice->curr_mb_addr)
52 return 0;
53 return h264_mb_slice_group(slice, mbaddr) == h264_mb_slice_group(slice, slice->curr_mb_addr);
56 uint32_t h264_next_mb_addr(struct h264_slice *slice, uint32_t mbaddr) {
57 int sg = h264_mb_slice_group(slice, mbaddr);
58 mbaddr++;
59 while (mbaddr < slice->pic_size_in_mbs && h264_mb_slice_group(slice, mbaddr) != sg)
60 mbaddr++;
61 return mbaddr;
64 static const struct h264_macroblock mb_unavail_intra = {
65 /* filled with "default" values assumed by prediction for unavailable mbs, to avoid special cases */
66 .mb_type = H264_MB_TYPE_UNAVAIL,
67 .mb_field_decoding_flag = 0,
68 .coded_block_pattern = 0x0f,
69 .transform_size_8x8_flag = 0,
70 .intra_chroma_pred_mode = 0,
71 .coded_block_flag = {
72 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
73 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
74 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
78 static const struct h264_macroblock mb_unavail_inter = {
79 /* filled with "default" values assumed by prediction for unavailable mbs, to avoid special cases */
80 .mb_type = H264_MB_TYPE_UNAVAIL,
81 .mb_field_decoding_flag = 0,
82 .coded_block_pattern = 0x0f,
83 .transform_size_8x8_flag = 0,
84 .intra_chroma_pred_mode = 0,
85 .coded_block_flag = { 0 },
88 const struct h264_macroblock *h264_mb_unavail(int inter) {
89 return (inter ? &mb_unavail_inter : &mb_unavail_intra);
92 const struct h264_macroblock *h264_mb_nb_p(struct h264_slice *slice, enum h264_mb_pos pos, int inter) {
93 uint32_t mbaddr = slice->curr_mb_addr;
94 if (slice->mbaff_frame_flag)
95 mbaddr /= 2;
96 switch (pos) {
97 case H264_MB_THIS:
98 return &slice->mbs[slice->curr_mb_addr];
99 case H264_MB_A:
100 if ((mbaddr % slice->pic_width_in_mbs) == 0)
101 return h264_mb_unavail(inter);
102 mbaddr--;
103 break;
104 case H264_MB_B:
105 mbaddr -= slice->pic_width_in_mbs;
106 break;
107 case H264_MB_C:
108 if (((mbaddr+1) % slice->pic_width_in_mbs) == 0)
109 return h264_mb_unavail(inter);
110 mbaddr -= slice->pic_width_in_mbs - 1;
111 break;
112 case H264_MB_D:
113 if ((mbaddr % slice->pic_width_in_mbs) == 0)
114 return h264_mb_unavail(inter);
115 mbaddr -= slice->pic_width_in_mbs + 1;
116 break;
118 if (slice->mbaff_frame_flag)
119 mbaddr *= 2;
120 if (!h264_mb_avail(slice, mbaddr))
121 return h264_mb_unavail(inter);
122 return &slice->mbs[mbaddr];
125 const struct h264_macroblock *h264_mb_nb(struct h264_slice *slice, enum h264_mb_pos pos, int inter) {
126 const struct h264_macroblock *mbp = h264_mb_nb_p(slice, pos, inter);
127 const struct h264_macroblock *mbt = &slice->mbs[slice->curr_mb_addr];
128 switch (pos) {
129 case H264_MB_THIS:
130 return mbp;
131 case H264_MB_A:
132 /* to the left */
133 /* if not MBAFF - simply use mbp */
134 /* if MBAFF and mbp not available - just pass mbp */
135 /* if MBAFF and mbp available and frame/field coding differs - use mbp[0] */
136 /* if MBAFF and mbp available and frame/field coding same - use mbp[curr_mb & 1] */
137 if (slice->mbaff_frame_flag
138 && mbp->mb_type != H264_MB_TYPE_UNAVAIL
139 && (slice->curr_mb_addr & 1)
140 && mbp->mb_field_decoding_flag == mbt->mb_field_decoding_flag)
141 return mbp + 1;
142 return mbp;
143 case H264_MB_B:
144 if (slice->mbaff_frame_flag) {
145 if (mbt->mb_field_decoding_flag) {
146 /* MBAFF and in field mb */
147 if (mbp->mb_type == H264_MB_TYPE_UNAVAIL)
148 return mbp;
149 /* MBAFF and in field mb, with mb pair above available */
150 /* if above mb pair is frame coded, use bottom mb */
151 /* if above is field coded, use mb of same parity */
152 if (!(slice->curr_mb_addr & 1)
153 && mbp->mb_field_decoding_flag)
154 return mbp;
155 else
156 return mbp+1;
157 } else {
158 /* MBAFF and in frame mb */
159 /* if in bottom mb of the pair, use the top mb */
160 if (slice->curr_mb_addr & 1)
161 return mbt-1;
162 /* otherwise, use the bottom mb of the above pair, whether field or frame */
163 else if (mbp->mb_type != H264_MB_TYPE_UNAVAIL)
164 return mbp+1;
165 else
166 return mbp;
169 return mbp;
170 default:
171 abort();
175 const struct h264_macroblock *h264_mb_nb_b(struct h264_slice *slice, enum h264_mb_pos pos, enum h264_block_size bs, int inter, int idx, int *pidx) {
176 const struct h264_macroblock *mbp = h264_mb_nb_p(slice, pos, inter);
177 const struct h264_macroblock *mbo = h264_mb_nb(slice, pos, inter);
178 const struct h264_macroblock *mbt = &slice->mbs[slice->curr_mb_addr];
179 if (slice->chroma_array_type == 1 && bs == H264_BLOCK_CHROMA)
180 bs = H264_BLOCK_8X8;
181 /* from now on BLOCK_CHROMA means 8x4 blocks */
182 enum {
183 M_SAME = 0,
184 M_FRAME_FROM_FIELD = 1,
185 M_FIELD_FROM_FRAME = 2,
186 } mode = M_SAME;
187 if (!mbt->mb_field_decoding_flag && mbp->mb_field_decoding_flag && mbp->mb_type != H264_MB_TYPE_UNAVAIL) {
188 mode = M_FRAME_FROM_FIELD;
190 if (mbt->mb_field_decoding_flag && !mbp->mb_field_decoding_flag && mbp->mb_type != H264_MB_TYPE_UNAVAIL) {
191 mode = M_FIELD_FROM_FRAME;
193 int par = slice->curr_mb_addr & 1;
194 switch(pos) {
195 case H264_MB_A:
196 switch (bs) {
197 case H264_BLOCK_4X4:
198 if (idx & 1) {
199 *pidx = idx - 1;
200 return mbt;
201 } else if (idx & 4) {
202 *pidx = idx - 3;
203 return mbt;
204 } else {
205 switch (mode) {
206 case M_SAME:
207 *pidx = idx + 5;
208 return mbo;
209 case M_FRAME_FROM_FIELD:
210 *pidx = (idx >> 2 & 2) + par * 8 + 5;
211 return mbo;
212 case M_FIELD_FROM_FRAME:
213 *pidx = (idx << 2 & 8) + 5;
214 return mbp + (idx >> 3);
217 case H264_BLOCK_CHROMA:
218 if (idx & 1) {
219 *pidx = idx - 1;
220 return mbt;
221 } else {
222 switch (mode) {
223 case M_SAME:
224 *pidx = idx + 1;
225 return mbo;
226 case M_FRAME_FROM_FIELD:
227 *pidx = (idx >> 1 & 2) + par * 4 + 1;
228 return mbo;
229 case M_FIELD_FROM_FRAME:
230 *pidx = (idx << 1 & 4) + 1;
231 return mbp + (idx >> 2);
234 case H264_BLOCK_8X8:
235 if (idx & 1) {
236 *pidx = idx - 1;
237 return mbt;
238 } else {
239 switch (mode) {
240 case M_SAME:
241 *pidx = idx + 1;
242 return mbo;
243 case M_FRAME_FROM_FIELD:
244 *pidx = par * 2 + 1;
245 return mbo;
246 case M_FIELD_FROM_FRAME:
247 *pidx = 1;
248 return mbp + (idx >> 1);
251 default:
252 abort();
254 case H264_MB_B:
255 switch (bs) {
256 case H264_BLOCK_4X4:
257 if (idx & 2) {
258 *pidx = idx - 2;
259 return mbt;
260 } else if (idx & 8) {
261 *pidx = idx - 6;
262 return mbt;
263 } else {
264 *pidx = idx + 10;
265 return mbo;
267 case H264_BLOCK_CHROMA:
268 if (idx & 6) {
269 *pidx = idx - 2;
270 return mbt;
271 } else {
272 *pidx = idx + 6;
273 return mbo;
275 case H264_BLOCK_8X8:
276 if (idx & 2) {
277 *pidx = idx - 2;
278 return mbt;
279 } else {
280 *pidx = idx + 2;
281 return mbo;
283 default:
284 abort();
286 default:
287 abort();
291 const struct h264_macroblock *h264_inter_filter(struct h264_slice *slice, const struct h264_macroblock *mb, int inter) {
292 if (!inter
293 && slice->picparm->constrained_intra_pred_flag
294 && slice->nal_unit_type == H264_NAL_UNIT_TYPE_SLICE_PART_A
295 && h264_is_inter_mb_type(mb->mb_type))
296 return h264_mb_unavail(1);
297 else
298 return mb;
302 /* part mode, part 0 type, part 1 type */
303 static const int mb_part_info[][3] = {
304 [H264_MB_TYPE_P_L0_16X16] = { 0, 1 },
305 [H264_MB_TYPE_B_L0_16X16] = { 0, 1 },
306 [H264_MB_TYPE_B_L1_16X16] = { 0, 2 },
307 [H264_MB_TYPE_B_BI_16X16] = { 0, 3 },
308 [H264_MB_TYPE_P_L0_L0_16X8] = { 1, 1, 1 },
309 [H264_MB_TYPE_B_L0_L0_16X8] = { 1, 1, 1 },
310 [H264_MB_TYPE_B_L0_L1_16X8] = { 1, 1, 2 },
311 [H264_MB_TYPE_B_L0_BI_16X8] = { 1, 1, 3 },
312 [H264_MB_TYPE_B_L1_L0_16X8] = { 1, 2, 1 },
313 [H264_MB_TYPE_B_L1_L1_16X8] = { 1, 2, 2 },
314 [H264_MB_TYPE_B_L1_BI_16X8] = { 1, 2, 3 },
315 [H264_MB_TYPE_B_BI_L0_16X8] = { 1, 3, 1 },
316 [H264_MB_TYPE_B_BI_L1_16X8] = { 1, 3, 2 },
317 [H264_MB_TYPE_B_BI_BI_16X8] = { 1, 3, 3 },
318 [H264_MB_TYPE_P_L0_L0_8X16] = { 2, 1, 1 },
319 [H264_MB_TYPE_B_L0_L0_8X16] = { 2, 1, 1 },
320 [H264_MB_TYPE_B_L0_L1_8X16] = { 2, 1, 2 },
321 [H264_MB_TYPE_B_L0_BI_8X16] = { 2, 1, 3 },
322 [H264_MB_TYPE_B_L1_L0_8X16] = { 2, 2, 1 },
323 [H264_MB_TYPE_B_L1_L1_8X16] = { 2, 2, 2 },
324 [H264_MB_TYPE_B_L1_BI_8X16] = { 2, 2, 3 },
325 [H264_MB_TYPE_B_BI_L0_8X16] = { 2, 3, 1 },
326 [H264_MB_TYPE_B_BI_L1_8X16] = { 2, 3, 2 },
327 [H264_MB_TYPE_B_BI_BI_8X16] = { 2, 3, 3 },
330 static const int sub_mb_part_info[][2] = {
331 [H264_SUB_MB_TYPE_B_DIRECT_8X8] = { 0, 0 },
332 [H264_SUB_MB_TYPE_P_L0_8X8] = { 0, 1 },
333 [H264_SUB_MB_TYPE_B_L0_8X8] = { 0, 1 },
334 [H264_SUB_MB_TYPE_B_L1_8X8] = { 0, 2 },
335 [H264_SUB_MB_TYPE_B_BI_8X8] = { 0, 3 },
336 [H264_SUB_MB_TYPE_P_L0_8X4] = { 1, 1 },
337 [H264_SUB_MB_TYPE_B_L0_8X4] = { 1, 1 },
338 [H264_SUB_MB_TYPE_B_L1_8X4] = { 1, 2 },
339 [H264_SUB_MB_TYPE_B_BI_8X4] = { 1, 3 },
340 [H264_SUB_MB_TYPE_P_L0_4X8] = { 2, 1 },
341 [H264_SUB_MB_TYPE_B_L0_4X8] = { 2, 1 },
342 [H264_SUB_MB_TYPE_B_L1_4X8] = { 2, 2 },
343 [H264_SUB_MB_TYPE_B_BI_4X8] = { 2, 3 },
344 [H264_SUB_MB_TYPE_P_L0_4X4] = { 3, 1 },
345 [H264_SUB_MB_TYPE_B_L0_4X4] = { 3, 1 },
346 [H264_SUB_MB_TYPE_B_L1_4X4] = { 3, 2 },
347 [H264_SUB_MB_TYPE_B_BI_4X4] = { 3, 3 },
350 static int infer_intra(struct bitstream *str, struct h264_macroblock *mb, int which) {
351 int i;
352 for (i = 0; i < 4; i++) {
353 if (vs_infer(str, &mb->ref_idx[which][i], 0)) return 1;
355 for (i = 0; i < 16; i++) {
356 if (vs_infers(str, &mb->mvd[which][i][0], 0)) return 1;
357 if (vs_infers(str, &mb->mvd[which][i][1], 0)) return 1;
359 return 0;
362 int h264_mb_pred(struct bitstream *str, struct h264_cabac_context *cabac, struct h264_slice *slice, struct h264_macroblock *mb) {
363 int i, j;
364 if (mb->mb_type < H264_MB_TYPE_P_BASE) {
365 if (!h264_is_intra_16x16_mb_type(mb->mb_type)) {
366 if (!mb->transform_size_8x8_flag) {
367 for (i = 0; i < 16; i++) {
368 if (h264_prev_intra_pred_mode_flag(str, cabac, &mb->prev_intra4x4_pred_mode_flag[i])) return 1;
369 if (!mb->prev_intra4x4_pred_mode_flag[i])
370 if (h264_rem_intra_pred_mode(str, cabac, &mb->rem_intra4x4_pred_mode[i])) return 1;
372 } else {
373 for (i = 0; i < 4; i++) {
374 if (h264_prev_intra_pred_mode_flag(str, cabac, &mb->prev_intra8x8_pred_mode_flag[i])) return 1;
375 if (!mb->prev_intra8x8_pred_mode_flag[i])
376 if (h264_rem_intra_pred_mode(str, cabac, &mb->rem_intra8x8_pred_mode[i])) return 1;
380 if (slice->chroma_array_type == 1 || slice->chroma_array_type == 2) {
381 if (h264_intra_chroma_pred_mode(str, cabac, &mb->intra_chroma_pred_mode)) return 1;
382 } else {
383 if (vs_infer(str, &mb->intra_chroma_pred_mode, 0)) return 1;
385 if (infer_intra(str, mb, 0)) return 1;
386 if (infer_intra(str, mb, 1)) return 1;
387 } else if (mb->mb_type != H264_MB_TYPE_B_DIRECT_16X16) {
388 int ifrom[4], pmode[4];
389 ifrom[0] = -1;
390 pmode[0] = mb_part_info[mb->mb_type][1];
391 switch (mb_part_info[mb->mb_type][0]) {
392 case 0: /* 16x16 */
393 ifrom[1] = 0;
394 ifrom[2] = 0;
395 ifrom[3] = 0;
396 break;
397 case 1: /* 16x8 */
398 ifrom[1] = 0;
399 ifrom[2] = -1;
400 ifrom[3] = 2;
401 pmode[2] = mb_part_info[mb->mb_type][2];
402 break;
403 case 2:
404 ifrom[1] = -1;
405 ifrom[2] = 0;
406 ifrom[3] = 1;
407 pmode[1] = mb_part_info[mb->mb_type][2];
408 break;
410 int max = slice->num_ref_idx_l0_active_minus1;
411 if (slice->mbaff_frame_flag && mb->mb_field_decoding_flag)
412 max *= 2, max++;
413 for (i = 0; i < 4; i++) {
414 if (ifrom[i] == -1) {
415 if (pmode[i] & 1) {
416 if (h264_ref_idx(str, cabac, i, 0, max, &mb->ref_idx[0][i])) return 1;
417 } else {
418 if (vs_infer(str, &mb->ref_idx[0][i], 0)) return 1;
420 } else {
421 if (vs_infer(str, &mb->ref_idx[0][i], mb->ref_idx[0][ifrom[i]])) return 1;
424 max = slice->num_ref_idx_l1_active_minus1;
425 if (slice->mbaff_frame_flag && mb->mb_field_decoding_flag)
426 max *= 2, max++;
427 for (i = 0; i < 4; i++) {
428 if (ifrom[i] == -1) {
429 if (pmode[i] & 2) {
430 if (h264_ref_idx(str, cabac, i, 1, max, &mb->ref_idx[1][i])) return 1;
431 } else {
432 if (vs_infer(str, &mb->ref_idx[1][i], 0)) return 1;
434 } else {
435 if (vs_infer(str, &mb->ref_idx[1][i], mb->ref_idx[1][ifrom[i]])) return 1;
438 for (i = 0; i < 4; i++) {
439 if (ifrom[i] == -1) {
440 if (pmode[i] & 1) {
441 if (h264_mvd(str, cabac, i * 4, 0, 0, &mb->mvd[0][i*4][0])) return 1;
442 if (h264_mvd(str, cabac, i * 4, 1, 0, &mb->mvd[0][i*4][1])) return 1;
443 } else {
444 if (vs_infers(str, &mb->mvd[0][i*4][0], 0)) return 1;
445 if (vs_infers(str, &mb->mvd[0][i*4][1], 0)) return 1;
447 } else {
448 if (vs_infers(str, &mb->mvd[0][i*4][0], mb->mvd[0][ifrom[i]*4][0])) return 1;
449 if (vs_infers(str, &mb->mvd[0][i*4][1], mb->mvd[0][ifrom[i]*4][1])) return 1;
451 for (j = 1; j < 4; j++) {
452 if (vs_infers(str, &mb->mvd[0][i*4+j][0], mb->mvd[0][i*4][0])) return 1;
453 if (vs_infers(str, &mb->mvd[0][i*4+j][1], mb->mvd[0][i*4][1])) return 1;
456 for (i = 0; i < 4; i++) {
457 if (ifrom[i] == -1) {
458 if (pmode[i] & 2) {
459 if (h264_mvd(str, cabac, i * 4, 0, 1, &mb->mvd[1][i*4][0])) return 1;
460 if (h264_mvd(str, cabac, i * 4, 1, 1, &mb->mvd[1][i*4][1])) return 1;
461 } else {
462 if (vs_infers(str, &mb->mvd[1][i*4][0], 0)) return 1;
463 if (vs_infers(str, &mb->mvd[1][i*4][1], 0)) return 1;
465 } else {
466 if (vs_infers(str, &mb->mvd[1][i*4][0], mb->mvd[1][ifrom[i]*4][0])) return 1;
467 if (vs_infers(str, &mb->mvd[1][i*4][1], mb->mvd[1][ifrom[i]*4][1])) return 1;
469 for (j = 1; j < 4; j++) {
470 if (vs_infers(str, &mb->mvd[1][i*4+j][0], mb->mvd[1][i*4][0])) return 1;
471 if (vs_infers(str, &mb->mvd[1][i*4+j][1], mb->mvd[1][i*4][1])) return 1;
474 if (vs_infer(str, &mb->intra_chroma_pred_mode, 0)) return 1;
475 } else {
476 if (vs_infer(str, &mb->intra_chroma_pred_mode, 0)) return 1;
477 if (infer_intra(str, mb, 0)) return 1;
478 if (infer_intra(str, mb, 1)) return 1;
480 return 0;
483 int h264_sub_mb_pred(struct bitstream *str, struct h264_cabac_context *cabac, struct h264_slice *slice, struct h264_macroblock *mb) {
484 int i;
485 int pmode[4];
486 int ifrom[16];
487 for (i = 0; i < 4; i++) {
488 if (h264_sub_mb_type(str, cabac, slice->slice_type, &mb->sub_mb_type[i])) return 1;
489 pmode[i] = sub_mb_part_info[mb->sub_mb_type[i]][1];
490 int sm = sub_mb_part_info[mb->sub_mb_type[i]][0];
491 ifrom[i*4] = -1;
492 switch (sm) {
493 case 0:
494 ifrom[i*4 + 1] = i*4;
495 ifrom[i*4 + 2] = i*4;
496 ifrom[i*4 + 3] = i*4;
497 break;
498 case 1:
499 ifrom[i*4 + 1] = i*4;
500 ifrom[i*4 + 2] = -1;
501 ifrom[i*4 + 3] = i*4 + 2;
502 break;
503 case 2:
504 ifrom[i*4 + 1] = -1;
505 ifrom[i*4 + 2] = i*4;
506 ifrom[i*4 + 3] = i*4 + 1;
507 break;
508 case 3:
509 ifrom[i*4 + 1] = -1;
510 ifrom[i*4 + 2] = -1;
511 ifrom[i*4 + 3] = -1;
512 break;
515 int max = slice->num_ref_idx_l0_active_minus1;
516 if (slice->mbaff_frame_flag && mb->mb_field_decoding_flag)
517 max *= 2, max++;
518 for (i = 0; i < 4; i++) {
519 if (pmode[i] & 1 && mb->mb_type != H264_MB_TYPE_P_8X8REF0) {
520 if (h264_ref_idx(str, cabac, i, 0, max, &mb->ref_idx[0][i])) return 1;
521 } else {
522 if (vs_infer(str, &mb->ref_idx[0][i], 0)) return 1;
525 max = slice->num_ref_idx_l1_active_minus1;
526 if (slice->mbaff_frame_flag && mb->mb_field_decoding_flag)
527 max *= 2, max++;
528 for (i = 0; i < 4; i++) {
529 if (pmode[i] & 2) {
530 if (h264_ref_idx(str, cabac, i, 1, max, &mb->ref_idx[1][i])) return 1;
531 } else {
532 if (vs_infer(str, &mb->ref_idx[1][i], 0)) return 1;
535 for (i = 0; i < 16; i++) {
536 if (ifrom[i] == -1) {
537 if (pmode[i/4] & 1) {
538 if (h264_mvd(str, cabac, i, 0, 0, &mb->mvd[0][i][0])) return 1;
539 if (h264_mvd(str, cabac, i, 1, 0, &mb->mvd[0][i][1])) return 1;
540 } else {
541 if (vs_infers(str, &mb->mvd[0][i][0], 0)) return 1;
542 if (vs_infers(str, &mb->mvd[0][i][1], 0)) return 1;
544 } else {
545 if (vs_infers(str, &mb->mvd[0][i][0], mb->mvd[0][ifrom[i]][0])) return 1;
546 if (vs_infers(str, &mb->mvd[0][i][1], mb->mvd[0][ifrom[i]][1])) return 1;
549 for (i = 0; i < 16; i++) {
550 if (ifrom[i] == -1) {
551 if (pmode[i/4] & 2) {
552 if (h264_mvd(str, cabac, i, 0, 1, &mb->mvd[1][i][0])) return 1;
553 if (h264_mvd(str, cabac, i, 1, 1, &mb->mvd[1][i][1])) return 1;
554 } else {
555 if (vs_infers(str, &mb->mvd[1][i][0], 0)) return 1;
556 if (vs_infers(str, &mb->mvd[1][i][1], 0)) return 1;
558 } else {
559 if (vs_infers(str, &mb->mvd[1][i][0], mb->mvd[1][ifrom[i]][0])) return 1;
560 if (vs_infers(str, &mb->mvd[1][i][1], mb->mvd[1][ifrom[i]][1])) return 1;
563 if (vs_infer(str, &mb->intra_chroma_pred_mode, 0)) return 1;
564 return 0;
567 int h264_macroblock_layer(struct bitstream *str, struct h264_cabac_context *cabac, struct h264_slice *slice, struct h264_macroblock *mb) {
568 struct h264_picparm *picparm = slice->picparm;
569 struct h264_seqparm *seqparm = slice->seqparm;
570 if (h264_mb_type(str, cabac, slice->slice_type, &mb->mb_type)) return 1;
571 if (mb->mb_type == H264_MB_TYPE_I_PCM) {
572 if (vs_align_byte(str, VS_ALIGN_0)) return 1;
573 int i;
574 for (i = 0; i < 256; i++)
575 if (vs_u(str, &mb->pcm_sample_luma[i], slice->bit_depth_luma_minus8 + 8)) return 1;
576 if (slice->chroma_array_type) {
577 for (i = 0; i < (64 << slice->chroma_array_type); i++)
578 if (vs_u(str, &mb->pcm_sample_chroma[i], slice->bit_depth_chroma_minus8 + 8)) return 1;
580 if (cabac)
581 if (h264_cabac_init_arith(str, cabac)) return 1;
582 if (vs_infers(str, &mb->mb_qp_delta, 0)) return 1;
583 if (vs_infer(str, &mb->transform_size_8x8_flag, 0)) return 1;
584 if (vs_infer(str, &mb->coded_block_pattern, 0x2f)) return 1;
585 if (vs_infer(str, &mb->intra_chroma_pred_mode, 0)) return 1;
586 if (infer_intra(str, mb, 0)) return 1;
587 if (infer_intra(str, mb, 1)) return 1;
588 for (i = 0; i < 17; i++) {
589 mb->coded_block_flag[0][i] = 1;
590 mb->coded_block_flag[1][i] = 1;
591 mb->coded_block_flag[2][i] = 1;
593 for (i = 0; i < 16; i++) {
594 mb->total_coeff[0][i] = 16;
595 mb->total_coeff[1][i] = 16;
596 mb->total_coeff[2][i] = 16;
598 } else {
599 int noSubMbPartSizeLessThan8x8Flag = 1;
600 if (h264_is_submb_mb_type(mb->mb_type)) {
601 if (h264_sub_mb_pred(str, cabac, slice, mb)) return 1;
602 int i;
603 for (i = 0; i < 4; i++) {
604 if (mb->sub_mb_type[i] != H264_SUB_MB_TYPE_B_DIRECT_8X8) {
605 if (sub_mb_part_info[mb->sub_mb_type[i]][0])
606 noSubMbPartSizeLessThan8x8Flag = 0;
607 } else {
608 if (!seqparm->direct_8x8_inference_flag)
609 noSubMbPartSizeLessThan8x8Flag = 0;
612 if (vs_infer(str, &mb->intra_chroma_pred_mode, 0)) return 1;
613 } else {
614 if (mb->mb_type == H264_MB_TYPE_I_NXN || mb->mb_type == H264_MB_TYPE_SI) {
615 if (picparm->transform_8x8_mode_flag) {
616 if (h264_transform_size_8x8_flag(str, cabac, &mb->transform_size_8x8_flag)) return 1;
617 } else {
618 if (vs_infer(str, &mb->transform_size_8x8_flag, 0)) return 1;
621 if (h264_mb_pred(str, cabac, slice, mb)) return 1;
623 if (mb->mb_type == H264_MB_TYPE_I_NXN || mb->mb_type == H264_MB_TYPE_SI || mb->mb_type >= H264_MB_TYPE_I_END) {
624 int has_chroma = slice->chroma_array_type < 3 && slice->chroma_array_type != 0;
625 if (h264_coded_block_pattern(str, cabac, mb->mb_type, has_chroma, &mb->coded_block_pattern)) return 1;
626 if (mb->mb_type >= H264_MB_TYPE_I_END) {
627 if ((mb->coded_block_pattern & 0xf) && picparm->transform_8x8_mode_flag && noSubMbPartSizeLessThan8x8Flag && (mb->mb_type != H264_MB_TYPE_B_DIRECT_16X16 || seqparm->direct_8x8_inference_flag)) {
628 if (h264_transform_size_8x8_flag(str, cabac, &mb->transform_size_8x8_flag)) return 1;
629 } else {
630 if (vs_infer(str, &mb->transform_size_8x8_flag, 0)) return 1;
633 } else {
634 int infer_cbp = (((mb->mb_type - H264_MB_TYPE_I_16X16_0_0_0) >> 2) % 3) << 4;
635 if (mb->mb_type >= H264_MB_TYPE_I_16X16_0_0_1)
636 infer_cbp |= 0xf;
637 if (vs_infer(str, &mb->coded_block_pattern, infer_cbp)) return 1;
638 if (vs_infer(str, &mb->transform_size_8x8_flag, 0)) return 1;
640 if (mb->coded_block_pattern || h264_is_intra_16x16_mb_type(mb->mb_type)) {
641 if (h264_mb_qp_delta(str, cabac, &mb->mb_qp_delta)) return 1;
642 } else {
643 if (vs_infers(str, &mb->mb_qp_delta, 0)) return 1;
645 if (h264_residual(str, cabac, slice, mb, 0, 15)) return 1;
647 return 0;
650 static int inferred_mb_field_decoding_flag(struct h264_slice *slice) {
651 if (slice->mbaff_frame_flag) {
652 const struct h264_macroblock *mbA = h264_mb_nb_p(slice, H264_MB_A, 0);
653 const struct h264_macroblock *mbB = h264_mb_nb_p(slice, H264_MB_B, 0);
654 if (mbA->mb_type != H264_MB_TYPE_UNAVAIL) {
655 return mbA->mb_field_decoding_flag;
656 } else if (mbB->mb_type != H264_MB_TYPE_UNAVAIL) {
657 return mbB->mb_field_decoding_flag;
658 } else {
659 return 0;
661 } else {
662 return slice->field_pic_flag;
666 static int infer_skip(struct bitstream *str, struct h264_slice *slice, struct h264_macroblock *mb) {
667 uint32_t skip_type = (slice->slice_type == H264_SLICE_TYPE_B ? H264_MB_TYPE_B_SKIP : H264_MB_TYPE_P_SKIP);
668 if (slice->mbaff_frame_flag) {
669 if (slice->curr_mb_addr & 1) {
670 if (h264_is_skip_mb_type(slice->mbs[slice->curr_mb_addr & ~1].mb_type)) {
671 int val = inferred_mb_field_decoding_flag(slice);
672 if (vs_infer(str, &mb[-1].mb_field_decoding_flag, val)) return 1;
674 if (vs_infer(str, &mb->mb_field_decoding_flag, mb[-1].mb_field_decoding_flag)) return 1;
676 } else {
677 if (vs_infer(str, &slice->mbs[slice->curr_mb_addr].mb_field_decoding_flag, slice->field_pic_flag)) return 1;
680 if (vs_infer(str, &mb->mb_type, skip_type)) return 1;
681 if (vs_infers(str, &mb->mb_qp_delta, 0)) return 1;
682 if (vs_infer(str, &mb->transform_size_8x8_flag, 0)) return 1;
683 if (vs_infer(str, &mb->coded_block_pattern, 0)) return 1;
684 if (vs_infer(str, &mb->intra_chroma_pred_mode, 0)) return 1;
685 if (infer_intra(str, mb, 0)) return 1;
686 if (infer_intra(str, mb, 1)) return 1;
687 int i;
688 for (i = 0; i < 17; i++) {
689 mb->coded_block_flag[0][i] = 0;
690 mb->coded_block_flag[1][i] = 0;
691 mb->coded_block_flag[2][i] = 0;
693 for (i = 0; i < 16; i++) {
694 mb->total_coeff[0][i] = 0;
695 mb->total_coeff[1][i] = 0;
696 mb->total_coeff[2][i] = 0;
698 return 0;
701 int h264_slice_data(struct bitstream *str, struct h264_slice *slice) {
702 slice->prev_mb_addr = -1;
703 slice->curr_mb_addr = slice->first_mb_in_slice * (1 + slice->mbaff_frame_flag);
704 if (str->dir == VS_DECODE)
705 slice->last_mb_in_slice = slice->curr_mb_addr;
706 uint32_t skip_type = (slice->slice_type == H264_SLICE_TYPE_B ? H264_MB_TYPE_B_SKIP : H264_MB_TYPE_P_SKIP);
707 if (slice->picparm->entropy_coding_mode_flag) {
708 if (vs_align_byte(str, VS_ALIGN_1)) return 1;
709 struct h264_cabac_context *cabac = h264_cabac_new(slice);
710 if (h264_cabac_init_arith(str, cabac)) { h264_cabac_destroy(cabac); return 1; }
711 while (1) {
712 uint32_t mb_skip_flag = 0;
713 if (slice->slice_type != H264_SLICE_TYPE_I && slice->slice_type != H264_SLICE_TYPE_SI) {
714 if (str->dir == VS_ENCODE) {
715 mb_skip_flag = slice->mbs[slice->curr_mb_addr].mb_type == skip_type;
717 /* mb_field_decoding_flag is decoded *after* mb_skip_flag in some circumstances, have to use an inferred value for CABAC prediction */
718 int save = slice->mbs[slice->curr_mb_addr].mb_field_decoding_flag;
719 int ival;
720 if (slice->mbaff_frame_flag
721 && slice->curr_mb_addr & 1
722 && slice->mbs[slice->curr_mb_addr - 1].mb_type != skip_type) {
723 ival = slice->mbs[slice->curr_mb_addr - 1].mb_field_decoding_flag;
724 } else {
725 ival = inferred_mb_field_decoding_flag(slice);
727 slice->mbs[slice->curr_mb_addr].mb_field_decoding_flag = ival;
728 if (h264_mb_skip_flag(str, cabac, &mb_skip_flag)) { h264_cabac_destroy(cabac); return 1; }
729 slice->mbs[slice->curr_mb_addr].mb_field_decoding_flag = save;
731 if (mb_skip_flag) {
732 if (infer_skip(str, slice, &slice->mbs[slice->curr_mb_addr])) { h264_cabac_destroy(cabac); return 1; }
733 } else {
734 if (slice->mbaff_frame_flag) {
735 uint32_t first_addr = slice->curr_mb_addr & ~1;
736 if (slice->curr_mb_addr == first_addr) {
737 if (h264_mb_field_decoding_flag(str, cabac, &slice->mbs[first_addr].mb_field_decoding_flag)) { h264_cabac_destroy(cabac); return 1; }
738 } else {
739 if (slice->mbs[first_addr].mb_type == skip_type) {
740 if (h264_mb_field_decoding_flag(str, cabac, &slice->mbs[first_addr].mb_field_decoding_flag)) { h264_cabac_destroy(cabac); return 1; }
742 if (vs_infer(str, &slice->mbs[first_addr + 1].mb_field_decoding_flag, slice->mbs[first_addr].mb_field_decoding_flag)) { h264_cabac_destroy(cabac); return 1; }
744 } else {
745 if (vs_infer(str, &slice->mbs[slice->curr_mb_addr].mb_field_decoding_flag, slice->field_pic_flag)) { h264_cabac_destroy(cabac); return 1; }
747 if (h264_macroblock_layer(str, cabac, slice, &slice->mbs[slice->curr_mb_addr])) { h264_cabac_destroy(cabac); return 1; }
749 if (!slice->mbaff_frame_flag || (slice->curr_mb_addr & 1)) {
750 uint32_t end_of_slice_flag = slice->last_mb_in_slice == slice->curr_mb_addr;
751 if (h264_cabac_terminate(str, cabac, &end_of_slice_flag)) { h264_cabac_destroy(cabac); return 1; }
752 if (end_of_slice_flag) {
753 slice->last_mb_in_slice = slice->curr_mb_addr;
754 h264_cabac_destroy(cabac);
755 /* XXX: cabac_zero_word crap */
756 return vs_align_byte(str, VS_ALIGN_0);
759 slice->prev_mb_addr = slice->curr_mb_addr;
760 if (str->dir == VS_DECODE)
761 slice->last_mb_in_slice = slice->curr_mb_addr;
762 slice->curr_mb_addr = h264_next_mb_addr(slice, slice->curr_mb_addr);
763 if (slice->curr_mb_addr >= slice->pic_size_in_mbs) {
764 fprintf(stderr, "MB index out of range!\n");
765 return 1;
768 } else {
769 while (1) {
770 int end = 0;
771 uint32_t mb_skip_run = 0;
772 if (slice->slice_type != H264_SLICE_TYPE_I && slice->slice_type != H264_SLICE_TYPE_SI) {
773 if (str->dir == VS_ENCODE) {
774 mb_skip_run = 0;
775 while (slice->mbs[slice->curr_mb_addr].mb_type == skip_type) {
776 mb_skip_run++;
777 if (infer_skip(str, slice, &slice->mbs[slice->curr_mb_addr])) return 1;
778 if (slice->curr_mb_addr == slice->last_mb_in_slice) {
779 end = 1;
780 break;
782 slice->prev_mb_addr = slice->curr_mb_addr;
783 slice->curr_mb_addr = h264_next_mb_addr(slice, slice->curr_mb_addr);
785 if (vs_ue(str, &mb_skip_run)) return 1;
786 if (end)
787 goto out_cavlc;
788 } else {
789 if (vs_ue(str, &mb_skip_run)) return 1;
790 while (mb_skip_run--) {
791 if (slice->curr_mb_addr >= slice->pic_size_in_mbs) {
792 fprintf(stderr, "MB index out of range!\n");
793 return 1;
795 slice->last_mb_in_slice = slice->curr_mb_addr;
796 slice->mbs[slice->curr_mb_addr].mb_type = skip_type;
797 if (infer_skip(str, slice, &slice->mbs[slice->curr_mb_addr])) return 1;
798 slice->prev_mb_addr = slice->curr_mb_addr;
799 slice->last_mb_in_slice = slice->curr_mb_addr;
800 slice->curr_mb_addr = h264_next_mb_addr(slice, slice->curr_mb_addr);
802 int more = vs_has_more_data(str);
803 if (more == -1)
804 return 1;
805 if (more == 0)
806 goto out_cavlc;
809 if (slice->curr_mb_addr >= slice->pic_size_in_mbs) {
810 fprintf(stderr, "MB index out of range!\n");
811 return 1;
813 if (slice->mbaff_frame_flag) {
814 uint32_t first_addr = slice->curr_mb_addr & ~1;
815 if (slice->curr_mb_addr == first_addr) {
816 if (h264_mb_field_decoding_flag(str, 0, &slice->mbs[first_addr].mb_field_decoding_flag)) return 1;
817 } else {
818 if (slice->mbs[first_addr].mb_type == skip_type)
819 if (h264_mb_field_decoding_flag(str, 0, &slice->mbs[first_addr].mb_field_decoding_flag)) return 1;
820 if (vs_infer(str, &slice->mbs[first_addr + 1].mb_field_decoding_flag, slice->mbs[first_addr].mb_field_decoding_flag)) return 1;
822 } else {
823 if (vs_infer(str, &slice->mbs[slice->curr_mb_addr].mb_field_decoding_flag, slice->field_pic_flag)) return 1;
825 if (h264_macroblock_layer(str, 0, slice, &slice->mbs[slice->curr_mb_addr])) return 1;
826 if(str->dir == VS_ENCODE) {
827 if (slice->last_mb_in_slice == slice->curr_mb_addr)
828 goto out_cavlc;
829 } else {
830 slice->last_mb_in_slice = slice->curr_mb_addr;
831 int more = vs_has_more_data(str);
832 if (more == -1)
833 return 1;
834 if (more == 0)
835 goto out_cavlc;
837 slice->prev_mb_addr = slice->curr_mb_addr;
838 if (str->dir == VS_DECODE)
839 slice->last_mb_in_slice = slice->curr_mb_addr;
840 slice->curr_mb_addr = h264_next_mb_addr(slice, slice->curr_mb_addr);
841 if (slice->curr_mb_addr >= slice->pic_size_in_mbs) {
842 fprintf(stderr, "MB index out of range!\n");
843 return 1;
846 out_cavlc:
847 if (vs_end(str)) return 1;
848 return 0;