2 * Copyright (C) 2011 Marcelina KoĆcielnicka <mwk@0x04.net>
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
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.
26 #include "h264_cabac.h"
31 int h264_mb_slice_group(struct h264_slice
*slice
, uint32_t mbaddr
) {
32 if (mbaddr
>= slice
->pic_size_in_mbs
)
34 if (!slice
->picparm
->num_slice_groups_minus1
)
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];
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
];
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
)
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
);
59 while (mbaddr
< slice
->pic_size_in_mbs
&& h264_mb_slice_group(slice
, mbaddr
) != sg
)
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,
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
)
98 return &slice
->mbs
[slice
->curr_mb_addr
];
100 if ((mbaddr
% slice
->pic_width_in_mbs
) == 0)
101 return h264_mb_unavail(inter
);
105 mbaddr
-= slice
->pic_width_in_mbs
;
108 if (((mbaddr
+1) % slice
->pic_width_in_mbs
) == 0)
109 return h264_mb_unavail(inter
);
110 mbaddr
-= slice
->pic_width_in_mbs
- 1;
113 if ((mbaddr
% slice
->pic_width_in_mbs
) == 0)
114 return h264_mb_unavail(inter
);
115 mbaddr
-= slice
->pic_width_in_mbs
+ 1;
118 if (slice
->mbaff_frame_flag
)
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
];
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
)
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
)
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
)
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)
162 /* otherwise, use the bottom mb of the above pair, whether field or frame */
163 else if (mbp
->mb_type
!= H264_MB_TYPE_UNAVAIL
)
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
)
181 /* from now on BLOCK_CHROMA means 8x4 blocks */
184 M_FRAME_FROM_FIELD
= 1,
185 M_FIELD_FROM_FRAME
= 2,
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;
201 } else if (idx
& 4) {
209 case M_FRAME_FROM_FIELD
:
210 *pidx
= (idx
>> 2 & 2) + par
* 8 + 5;
212 case M_FIELD_FROM_FRAME
:
213 *pidx
= (idx
<< 2 & 8) + 5;
214 return mbp
+ (idx
>> 3);
217 case H264_BLOCK_CHROMA
:
226 case M_FRAME_FROM_FIELD
:
227 *pidx
= (idx
>> 1 & 2) + par
* 4 + 1;
229 case M_FIELD_FROM_FRAME
:
230 *pidx
= (idx
<< 1 & 4) + 1;
231 return mbp
+ (idx
>> 2);
243 case M_FRAME_FROM_FIELD
:
246 case M_FIELD_FROM_FRAME
:
248 return mbp
+ (idx
>> 1);
260 } else if (idx
& 8) {
267 case H264_BLOCK_CHROMA
:
291 const struct h264_macroblock
*h264_inter_filter(struct h264_slice
*slice
, const struct h264_macroblock
*mb
, int 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);
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
) {
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;
362 int h264_mb_pred(struct bitstream
*str
, struct h264_cabac_context
*cabac
, struct h264_slice
*slice
, struct h264_macroblock
*mb
) {
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;
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;
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];
390 pmode
[0] = mb_part_info
[mb
->mb_type
][1];
391 switch (mb_part_info
[mb
->mb_type
][0]) {
401 pmode
[2] = mb_part_info
[mb
->mb_type
][2];
407 pmode
[1] = mb_part_info
[mb
->mb_type
][2];
410 int max
= slice
->num_ref_idx_l0_active_minus1
;
411 if (slice
->mbaff_frame_flag
&& mb
->mb_field_decoding_flag
)
413 for (i
= 0; i
< 4; i
++) {
414 if (ifrom
[i
] == -1) {
416 if (h264_ref_idx(str
, cabac
, i
, 0, max
, &mb
->ref_idx
[0][i
])) return 1;
418 if (vs_infer(str
, &mb
->ref_idx
[0][i
], 0)) return 1;
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
)
427 for (i
= 0; i
< 4; i
++) {
428 if (ifrom
[i
] == -1) {
430 if (h264_ref_idx(str
, cabac
, i
, 1, max
, &mb
->ref_idx
[1][i
])) return 1;
432 if (vs_infer(str
, &mb
->ref_idx
[1][i
], 0)) return 1;
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) {
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;
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;
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) {
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;
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;
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;
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;
483 int h264_sub_mb_pred(struct bitstream
*str
, struct h264_cabac_context
*cabac
, struct h264_slice
*slice
, struct h264_macroblock
*mb
) {
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];
494 ifrom
[i
*4 + 1] = i
*4;
495 ifrom
[i
*4 + 2] = i
*4;
496 ifrom
[i
*4 + 3] = i
*4;
499 ifrom
[i
*4 + 1] = i
*4;
501 ifrom
[i
*4 + 3] = i
*4 + 2;
505 ifrom
[i
*4 + 2] = i
*4;
506 ifrom
[i
*4 + 3] = i
*4 + 1;
515 int max
= slice
->num_ref_idx_l0_active_minus1
;
516 if (slice
->mbaff_frame_flag
&& mb
->mb_field_decoding_flag
)
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;
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
)
528 for (i
= 0; i
< 4; i
++) {
530 if (h264_ref_idx(str
, cabac
, i
, 1, max
, &mb
->ref_idx
[1][i
])) return 1;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
608 if (!seqparm
->direct_8x8_inference_flag
)
609 noSubMbPartSizeLessThan8x8Flag
= 0;
612 if (vs_infer(str
, &mb
->intra_chroma_pred_mode
, 0)) return 1;
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;
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;
630 if (vs_infer(str
, &mb
->transform_size_8x8_flag
, 0)) return 1;
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
)
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;
643 if (vs_infers(str
, &mb
->mb_qp_delta
, 0)) return 1;
645 if (h264_residual(str
, cabac
, slice
, mb
, 0, 15)) return 1;
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
;
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;
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;
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;
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; }
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
;
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
;
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
;
732 if (infer_skip(str
, slice
, &slice
->mbs
[slice
->curr_mb_addr
])) { h264_cabac_destroy(cabac
); return 1; }
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; }
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; }
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");
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
) {
775 while (slice
->mbs
[slice
->curr_mb_addr
].mb_type
== skip_type
) {
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
) {
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;
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");
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
);
809 if (slice
->curr_mb_addr
>= slice
->pic_size_in_mbs
) {
810 fprintf(stderr
, "MB index out of range!\n");
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;
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;
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
)
830 slice
->last_mb_in_slice
= slice
->curr_mb_addr
;
831 int more
= vs_has_more_data(str
);
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");
847 if (vs_end(str
)) return 1;