28 #define UNCHECKED_BITSTREAM_READER 1
30 #include "libavutil/imgutils.h"
31 #include "libavutil/opt.h"
48 #include "libavutil/avassert.h"
56 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
57 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
58 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
59 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
64 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
65 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
66 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10,
67 10,10,10,11,11,11,11,11,11,12,12,12,12,12,12,13,13,13, 13, 13, 13,
72 #if CONFIG_H264_DXVA2_HWACCEL
75 #if CONFIG_H264_VAAPI_HWACCEL
78 #if CONFIG_H264_VDA_HWACCEL
81 #if CONFIG_H264_VDPAU_HWACCEL
89 #if CONFIG_H264_DXVA2_HWACCEL
92 #if CONFIG_H264_VAAPI_HWACCEL
95 #if CONFIG_H264_VDA_HWACCEL
98 #if CONFIG_H264_VDPAU_HWACCEL
113 int mb_x,
int mb_y,
int mb_intra,
int mb_skipped)
179 offset[2] = (y >> vshift) * src->
linesize[1];
208 for (i = 0; i < 2; i++) {
258 const int b4_stride = h->
mb_width * 4 + 1;
259 const int b4_array_size = b4_stride * h->
mb_height * 4;
285 (big_mb_num + h->
mb_stride) *
sizeof(uint32_t),
290 for (i = 0; i < 2; i++) {
292 2 * (b4_array_size + 4) *
sizeof(int16_t),
296 4 * mb_array_size *
sizeof(
uint8_t), fail)
308 return (ret < 0) ? ret :
AVERROR(ENOMEM);
351 static const int8_t top[12] = {
354 static const int8_t left[12] = {
360 for (i = 0; i < 4; i++) {
364 "top block unavailable for requested intra4x4 mode %d at %d %d\n",
374 static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
375 for (i = 0; i < 4; i++)
380 "left block unavailable for requested intra4x4 mode %d at %d %d\n",
403 "out of range intra chroma pred mode at %d %d\n",
412 "top block unavailable for requested intra mode at %d %d\n",
422 "left block unavailable for requested intra mode at %d %d\n",
438 int *dst_length,
int *consumed,
int length)
451 #define STARTCODE_TEST \
452 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
453 if (src[i + 2] != 3) { \
459 #if HAVE_FAST_UNALIGNED
460 #define FIND_FIRST_ZERO \
461 if (i > 0 && !src[i]) \
466 for (i = 0; i + 1 < length; i += 9) {
468 (
AV_RN64A(src + i) - 0x0100010001000101ULL)) &
469 0x8000800080008080ULL))
476 for (i = 0; i + 1 < length; i += 5) {
478 (
AV_RN32A(src + i) - 0x01000101U)) &
487 for (i = 0; i + 1 < length; i += 2) {
490 if (i > 0 && src[i - 1] == 0)
512 memcpy(dst, src, length);
519 while (si + 2 < length) {
521 if (src[si + 2] > 3) {
522 dst[di++] = src[si++];
523 dst[di++] = src[si++];
524 }
else if (src[si] == 0 && src[si + 1] == 0) {
525 if (src[si + 2] == 3) {
534 dst[di++] = src[si++];
537 dst[di++] = src[si++];
560 for (r = 1; r < 9; r++) {
569 int height,
int y_offset,
int list)
572 int filter_height_down = (raw_my & 3) ? 3 : 0;
573 int full_my = (raw_my >> 2) + y_offset;
574 int bottom = full_my + filter_height_down +
height;
578 return FFMAX(0, bottom);
582 int height,
int y_offset,
int list0,
583 int list1,
int *nrefs)
599 if (refs[0][ref_n] < 0)
601 refs[0][ref_n] =
FFMAX(refs[0][ref_n], my);
612 if (refs[1][ref_n] < 0)
614 refs[1][ref_n] =
FFMAX(refs[1][ref_n], my);
626 const int mb_xy = h->
mb_xy;
629 int nrefs[2] = { 0 };
632 memset(refs, -1,
sizeof(refs));
652 for (i = 0; i < 4; i++) {
655 int y_offset = (i & 2) << 2;
659 IS_DIR(sub_mb_type, 0, 0),
660 IS_DIR(sub_mb_type, 0, 1),
664 IS_DIR(sub_mb_type, 0, 0),
665 IS_DIR(sub_mb_type, 0, 1),
668 IS_DIR(sub_mb_type, 0, 0),
669 IS_DIR(sub_mb_type, 0, 1),
673 IS_DIR(sub_mb_type, 0, 0),
674 IS_DIR(sub_mb_type, 0, 1),
677 IS_DIR(sub_mb_type, 0, 0),
678 IS_DIR(sub_mb_type, 0, 1),
683 for (j = 0; j < 4; j++) {
684 int sub_y_offset = y_offset + 2 * (j & 2);
686 IS_DIR(sub_mb_type, 0, 0),
687 IS_DIR(sub_mb_type, 0, 1),
694 for (list = h->
list_count - 1; list >= 0; list--)
695 for (ref = 0; ref < 48 && nrefs[list]; ref++) {
696 int row = refs[list][ref];
701 int pic_height = 16 * h->
mb_height >> ref_field_picture;
708 FFMIN((row >> 1) - !(row & 1),
712 FFMIN((row >> 1), pic_height - 1),
716 FFMIN(row * 2 + ref_field,
721 FFMIN(row, pic_height - 1),
725 FFMIN(row, pic_height - 1),
737 int src_x_offset,
int src_y_offset,
740 int pixel_shift,
int chroma_idc)
742 const int mx = h->
mv_cache[list][
scan8[n]][0] + src_x_offset * 8;
744 const int luma_xy = (mx & 3) + ((my & 3) << 2);
749 int extra_height = 0;
751 const int full_mx = mx >> 2;
752 const int full_my = my >> 2;
753 const int pic_width = 16 * h->
mb_width;
762 if (full_mx < 0 - extra_width ||
763 full_my < 0 - extra_height ||
764 full_mx + 16 > pic_width + extra_width ||
765 full_my + 16 > pic_height + extra_height) {
769 16 + 5, 16 + 5 , full_mx - 2,
770 full_my - 2, pic_width, pic_height);
782 if (chroma_idc == 3 ) {
789 full_mx - 2, full_my - 2,
790 pic_width, pic_height);
803 full_mx - 2, full_my - 2,
804 pic_width, pic_height);
813 ysh = 3 - (chroma_idc == 2 );
817 emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
820 src_cb = pic->
f.
data[1] + ((mx >> 3) << pixel_shift) +
822 src_cr = pic->
f.
data[2] + ((mx >> 3) << pixel_shift) +
827 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
828 pic_width >> 1, pic_height >> (chroma_idc == 1 ));
832 height >> (chroma_idc == 1 ),
833 mx & 7, (my << (chroma_idc == 2 )) & 7);
837 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
838 pic_width >> 1, pic_height >> (chroma_idc == 1 ));
841 chroma_op(dest_cr, src_cr, h->
mb_uvlinesize, height >> (chroma_idc == 1 ),
842 mx & 7, (my << (chroma_idc == 2 )) & 7);
849 int x_offset,
int y_offset,
854 int list0,
int list1,
855 int pixel_shift,
int chroma_idc)
860 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
861 if (chroma_idc == 3 ) {
862 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
863 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
864 }
else if (chroma_idc == 2 ) {
865 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
866 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
868 dest_cb += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
869 dest_cr += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
871 x_offset += 8 * h->
mb_x;
877 dest_y, dest_cb, dest_cr, x_offset, y_offset,
878 qpix_op, chroma_op, pixel_shift, chroma_idc);
881 chroma_op = chroma_avg;
887 dest_y, dest_cb, dest_cr, x_offset, y_offset,
888 qpix_op, chroma_op, pixel_shift, chroma_idc);
896 int x_offset,
int y_offset,
903 int list0,
int list1,
904 int pixel_shift,
int chroma_idc)
908 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
909 if (chroma_idc == 3 ) {
911 chroma_weight_avg = luma_weight_avg;
912 chroma_weight_op = luma_weight_op;
913 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
914 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
915 }
else if (chroma_idc == 2 ) {
917 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
918 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
920 chroma_height = height >> 1;
921 dest_cb += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
922 dest_cr += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
924 x_offset += 8 * h->
mb_x;
927 if (list0 && list1) {
937 dest_y, dest_cb, dest_cr,
938 x_offset, y_offset, qpix_put, chroma_put,
939 pixel_shift, chroma_idc);
941 tmp_y, tmp_cb, tmp_cr,
942 x_offset, y_offset, qpix_put, chroma_put,
943 pixel_shift, chroma_idc);
947 int weight1 = 64 - weight0;
949 height, 5, weight0, weight1, 0);
951 chroma_height, 5, weight0, weight1, 0);
953 chroma_height, 5, weight0, weight1, 0);
955 luma_weight_avg(dest_y, tmp_y, h->
mb_linesize, height,
961 chroma_weight_avg(dest_cb, tmp_cb, h->
mb_uvlinesize, chroma_height,
967 chroma_weight_avg(dest_cr, tmp_cr, h->
mb_uvlinesize, chroma_height,
975 int list = list1 ? 1 : 0;
978 mc_dir_part(h, ref, n, square, height, delta, list,
979 dest_y, dest_cb, dest_cr, x_offset, y_offset,
980 qpix_put, chroma_put, pixel_shift, chroma_idc);
1000 int pixel_shift,
int chroma_idc)
1009 int off = (mx << pixel_shift) +
1011 (64 << pixel_shift);
1013 if (chroma_idc == 3 ) {
1017 off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (h->
mb_x&7))*h->
uvlinesize;
1042 for (i = 0; i < 3; i++)
1051 }
else if (h->
DPB) {
1090 for (i = 0; i < 6; i++) {
1092 for (j = 0; j < i; j++)
1101 for (q = 0; q < max_qp + 1; q++) {
1104 for (x = 0; x < 64; x++)
1116 for (i = 0; i < 6; i++) {
1118 for (j = 0; j < i; j++)
1127 for (q = 0; q < max_qp + 1; q++) {
1130 for (x = 0; x < 16; x++)
1145 for (i = 0; i < 6; i++)
1146 for (x = 0; x < 16; x++)
1149 for (i = 0; i < 6; i++)
1150 for (x = 0; x < 64; x++)
1162 row_mb_num * 8 *
sizeof(
uint8_t), fail)
1164 big_mb_num * 48 *
sizeof(
uint8_t), fail)
1168 big_mb_num *
sizeof(uint16_t), fail)
1170 big_mb_num *
sizeof(
uint8_t), fail)
1172 16 * row_mb_num *
sizeof(
uint8_t), fail);
1174 16 * row_mb_num *
sizeof(
uint8_t), fail);
1176 4 * big_mb_num *
sizeof(
uint8_t), fail);
1178 big_mb_num *
sizeof(
uint8_t), fail)
1185 big_mb_num *
sizeof(uint32_t), fail);
1187 big_mb_num *
sizeof(uint32_t), fail);
1189 for (x = 0; x < h->
mb_width; x++) {
1191 const int b_xy = 4 * x + 4 * y * h->
b_stride;
1253 int yc_size = y_size + 2 * c_size;
1292 mb_array_size *
sizeof(
uint8_t), fail);
1306 for (i = 0; i < yc_size; i++)
1317 int parse_extradata);
1354 if (!buf || size <= 0)
1358 int i, cnt, nalsize;
1359 const unsigned char *p = buf;
1371 cnt = *(p + 5) & 0x1f;
1373 for (i = 0; i < cnt; i++) {
1375 if(nalsize > size - (p-buf))
1379 "Decoding sps %d from avcC failed\n", i);
1386 for (i = 0; i < cnt; i++) {
1388 if(nalsize > size - (p-buf))
1392 "Decoding pps %d from avcC failed\n", i);
1469 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
1470 #undef REBASE_PICTURE
1471 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
1472 ((pic && pic >= old_ctx->DPB && \
1473 pic < old_ctx->DPB + old_ctx->picture_count) ? \
1474 &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
1482 for (i = 0; i < count; i++) {
1483 assert((
IN_RANGE(from[i], old_base,
sizeof(*old_base)) ||
1495 for (i = 0; i < count; i++) {
1496 if (to[i] && !from[i])
1498 else if (from[i] && !to[i])
1502 memcpy(to[i], from[i], size);
1520 #define copy_fields(to, from, start_field, end_field) \
1521 memcpy(&to->start_field, &from->start_field, \
1522 (char *)&to->end_field - (char *)&to->start_field)
1533 int context_reinitialized = 0;
1540 (h->
width != h1->width ||
1541 h->
height != h1->height ||
1550 h->
width = h1->width;
1569 context_reinitialized = 1;
1592 memcpy(h, h1, offsetof(
H264Context, intra_pcm_ptr));
1593 memcpy(&h->
cabac, &h1->cabac,
1600 memset(&h->
er, 0,
sizeof(h->
er));
1601 memset(&h->
me, 0,
sizeof(h->
me));
1616 if (h1->context_initialized) {
1631 for (i = 0; i < 2; i++) {
1654 memcpy(h->
DPB, h1->DPB, h1->picture_count *
sizeof(*h1->DPB));
1683 copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1685 for (i = 0; i < 6; i++)
1687 (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1689 for (i = 0; i < 6; i++)
1691 (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1710 if (context_reinitialized)
1786 for (i = 0; i < 16; i++) {
1790 for (i = 0; i < 16; i++) {
1830 int i, pics, out_of_order, out_idx;
1931 for (i = 0; 1; i++) {
1943 out_of_order =
FFMAX(out_of_order, 1);
2009 int linesize,
int uvlinesize,
2019 src_cb -= uvlinesize;
2020 src_cr -= uvlinesize;
2026 AV_COPY128(top_border, src_y + 15 * linesize);
2028 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
2032 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
2033 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
2034 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
2035 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
2037 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
2038 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
2040 }
else if (chroma422) {
2042 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
2043 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
2045 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
2046 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
2050 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
2051 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
2053 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
2054 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
2068 AV_COPY128(top_border, src_y + 16 * linesize);
2070 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
2075 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
2076 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
2077 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
2078 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
2080 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
2081 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
2083 }
else if (chroma422) {
2085 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
2086 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
2088 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
2089 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
2093 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
2094 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
2096 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
2097 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
2105 int linesize,
int uvlinesize,
2106 int xchg,
int chroma444,
2107 int simple,
int pixel_shift)
2109 int deblock_topleft;
2128 deblock_topleft = (h->
mb_x > 0);
2132 src_y -= linesize + 1 + pixel_shift;
2133 src_cb -= uvlinesize + 1 + pixel_shift;
2134 src_cr -= uvlinesize + 1 + pixel_shift;
2139 #define XCHG(a, b, xchg) \
2140 if (pixel_shift) { \
2142 AV_SWAP64(b + 0, a + 0); \
2143 AV_SWAP64(b + 8, a + 8); \
2153 if (deblock_topleft) {
2154 XCHG(top_border_m1 + (8 << pixel_shift),
2155 src_y - (7 << pixel_shift), 1);
2157 XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
2158 XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
2161 src_y + (17 << pixel_shift), 1);
2166 if (deblock_topleft) {
2167 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2168 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2170 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
2171 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
2172 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
2173 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
2175 XCHG(h->
top_borders[top_idx][h->
mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
2176 XCHG(h->
top_borders[top_idx][h->
mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
2180 if (deblock_topleft) {
2181 XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2182 XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2184 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
2185 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
2194 if (high_bit_depth) {
2203 if (high_bit_depth) {
2210 int mb_type,
int is_h264,
2212 int transform_bypass,
2222 block_offset += 16 * p;
2225 if (transform_bypass) {
2232 for (i = 0; i < 16; i += 4) {
2233 uint8_t *
const ptr = dest_y + block_offset[i];
2236 h->
hpc.
pred8x8l_add[dir](ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2242 if (nnz == 1 &&
dctcoef_get(h->
mb, pixel_shift, i * 16 + p * 256))
2243 idct_dc_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2245 idct_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2250 if (transform_bypass) {
2257 for (i = 0; i < 16; i++) {
2258 uint8_t *
const ptr = dest_y + block_offset[i];
2262 h->
hpc.
pred4x4_add[dir](ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2270 if (!topright_avail) {
2272 tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
2273 topright = (
uint8_t *)&tr_high;
2275 tr = ptr[3 - linesize] * 0x01010101
u;
2279 topright = ptr + (4 << pixel_shift) - linesize;
2283 h->
hpc.
pred4x4[dir](ptr, topright, linesize);
2287 if (nnz == 1 &&
dctcoef_get(h->
mb, pixel_shift, i * 16 + p * 256))
2288 idct_dc_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2290 idct_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2301 if (!transform_bypass)
2306 static const uint8_t dc_mapping[16] = {
2307 0 * 16, 1 * 16, 4 * 16, 5 * 16,
2308 2 * 16, 3 * 16, 6 * 16, 7 * 16,
2309 8 * 16, 9 * 16, 12 * 16, 13 * 16,
2310 10 * 16, 11 * 16, 14 * 16, 15 * 16 };
2311 for (i = 0; i < 16; i++)
2313 pixel_shift, dc_mapping[i],
2325 int is_h264,
int simple,
2326 int transform_bypass,
2334 block_offset += 16 * p;
2338 if (transform_bypass) {
2343 h->
mb + (p * 256 << pixel_shift),
2346 for (i = 0; i < 16; i++)
2350 h->
mb + (i * 16 + p * 256 << pixel_shift),
2355 h->
mb + (p * 256 << pixel_shift),
2359 }
else if (h->
cbp & 15) {
2360 if (transform_bypass) {
2361 const int di =
IS_8x8DCT(mb_type) ? 4 : 1;
2364 for (i = 0; i < 16; i += di)
2367 h->
mb + (i * 16 + p * 256 << pixel_shift),
2372 h->
mb + (p * 256 << pixel_shift),
2377 h->
mb + (p * 256 << pixel_shift),
2383 for (i = 0; i < 16; i++)
2386 uint8_t *
const ptr = dest_y + block_offset[i];
2408 const int mb_xy = h->
mb_xy;
2414 hl_decode_mb_444_complex(h);
2416 hl_decode_mb_444_simple_8(h);
2417 }
else if (is_complex) {
2418 hl_decode_mb_complex(h);
2420 hl_decode_mb_simple_16(h);
2422 hl_decode_mb_simple_8(h);
2428 int luma_def, chroma_def;
2448 for (list = 0; list < 2; list++) {
2451 for (i = 0; i < h->
ref_count[list]; i++) {
2452 int luma_weight_flag, chroma_weight_flag;
2455 if (luma_weight_flag) {
2470 if (chroma_weight_flag) {
2472 for (j = 0; j < 2; j++) {
2483 for (j = 0; j < 2; j++) {
2504 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
2506 for (i = 0; i < 2; i++) {
2538 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
2540 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
2544 int td = av_clip(poc1 - poc0, -128, 127);
2546 int tb = av_clip(cur_poc - poc0, -128, 127);
2547 int tx = (16384 + (
FFABS(td) >> 1)) / td;
2548 int dist_scale_factor = (tb * tx + 32) >> 8;
2549 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
2550 w = 64 - dist_scale_factor;
2661 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
2672 expected_delta_per_poc_cycle = 0;
2677 if (abs_frame_num > 0) {
2681 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
2682 for (i = 0; i <= frame_num_in_poc_cycle; i++)
2690 field_poc[0] = expectedpoc + h->
delta_poc[0];
2720 for (i = 0; i < 16; i++) {
2721 #define T(x) (x >> 2) | ((x << 2) & 0xF)
2726 for (i = 0; i < 64; i++) {
2727 #define T(x) (x >> 3) | ((x & 7) << 3)
2779 "hardware accelerator failed to decode picture\n");
2872 "Reenabling low delay requires a codec flush.\n");
2882 "Different bit depth between chroma and luma", 1);
2892 "VDPAU decoding does not support video colorspace.\n");
3047 "Could not allocate memory for h264\n");
3058 " reducing to %d\n", nb_slices, max_slices);
3059 nb_slices = max_slices;
3143 unsigned int first_mb_in_slice;
3144 unsigned int pps_id;
3145 int num_ref_idx_active_override_flag, ret;
3146 unsigned int slice_type, tmp, i, j;
3147 int last_pic_structure, last_pic_droppable;
3149 int needs_reinit = 0;
3151 int frame_num, picture_structure, droppable;
3159 if (first_mb_in_slice == 0) {
3176 if (slice_type > 9) {
3178 "slice type too large (%d) at %d %d\n",
3182 if (slice_type > 4) {
3202 "non-existing PPS %u referenced\n",
3211 "non-existing SPS %u referenced\n",
3223 "SPS changed in the middle of the frame\n");
3312 "h264_slice_header_init() failed\n");
3319 "Cannot (re-)initialize context during parallel decoding.\n");
3329 "h264_slice_header_init() failed\n");
3368 if (last_pic_structure != picture_structure ||
3369 last_pic_droppable != droppable) {
3371 "Changing field mode (%d -> %d) between slices is not allowed\n",
3376 "unset cur_pic_ptr on %d. slice\n",
3394 if (unwrap_prev_frame_num > h->
frame_num)
3395 unwrap_prev_frame_num -= max_frame_num;
3399 if (unwrap_prev_frame_num < 0)
3400 unwrap_prev_frame_num += max_frame_num;
3447 "Invalid field mode combination %d/%d\n",
3452 }
else if (last_pic_droppable != h->
droppable) {
3454 "Cannot combine reference and non-reference fields in the same frame\n");
3577 first_mb_in_slice >= h->
mb_num) {
3627 num_ref_idx_active_override_flag =
get_bits1(&h->
gb);
3629 if (num_ref_idx_active_override_flag) {
3674 for (i = 0; i < 2; i++) {
3737 "deblocking_filter_idc %u out of range\n", tmp);
3750 "deblocking filter parameters %d %d out of range\n",
3775 "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
3780 "Deblocking switched inside frame.\n");
3805 for (j = 0; j < 2; j++) {
3808 for (i = 0; i < 16; i++) {
3828 for (i = 0; i < 16; i++)
3829 ref2frm[i + 2] = 4 * id_list[i] +
3832 ref2frm[18 + 1] = -1;
3833 for (i = 16; i < 48; i++)
3834 ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
3844 "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
3885 int mb_type,
int top_xy,
3888 int left_type[LEFT_MBS],
3889 int mb_xy,
int list)
3896 const int b_xy = h->
mb2b_xy[top_xy] + 3 * b_stride;
3897 const int b8_xy = 4 * top_xy + 2;
3900 ref_cache[0 - 1 * 8] =
3902 ref_cache[2 - 1 * 8] =
3912 const int b8_xy = 4 * left_xy[
LTOP] + 1;
3920 ref_cache[-1 + 16] =
3921 ref_cache[-1 + 24] = ref2frm[list][h->
cur_pic.
f.
ref_index[list][b8_xy + 2 * 1]];
3929 ref_cache[-1 + 16] =
3947 uint32_t ref01 = (
pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
3948 uint32_t ref23 = (
pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
3949 AV_WN32A(&ref_cache[0 * 8], ref01);
3950 AV_WN32A(&ref_cache[1 * 8], ref01);
3951 AV_WN32A(&ref_cache[2 * 8], ref23);
3952 AV_WN32A(&ref_cache[3 * 8], ref23);
3957 AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
3958 AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
3959 AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
3960 AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
3970 const int mb_xy = h->
mb_xy;
3981 left_xy[
LBOT] = left_xy[
LTOP] = mb_xy - 1;
3986 if (left_mb_field_flag != curr_mb_field_flag)
3989 if (curr_mb_field_flag)
3992 if (left_mb_field_flag != curr_mb_field_flag)
4006 if (qp <= qp_thresh &&
4007 (left_xy[
LTOP] < 0 ||
4013 if ((left_xy[
LTOP] < 0 ||
4028 left_type[
LTOP] = left_type[
LBOT] = 0;
4033 left_type[
LTOP] = left_type[
LBOT] = 0;
4043 top_type, left_type, mb_xy, 0);
4046 top_type, left_type, mb_xy, 1);
4050 AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
4051 AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
4052 AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
4053 AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
4058 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
4061 if (left_type[
LTOP]) {
4063 nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
4064 nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
4065 nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
4066 nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
4073 nnz_cache[4 + 8 * 0] =
4074 nnz_cache[5 + 8 * 0] = (h->
cbp_table[top_xy] & 0x4000) >> 12;
4075 nnz_cache[6 + 8 * 0] =
4076 nnz_cache[7 + 8 * 0] = (h->
cbp_table[top_xy] & 0x8000) >> 12;
4079 nnz_cache[3 + 8 * 1] =
4080 nnz_cache[3 + 8 * 2] = (h->
cbp_table[left_xy[
LTOP]] & 0x2000) >> 12;
4083 nnz_cache[3 + 8 * 3] =
4084 nnz_cache[3 + 8 * 4] = (h->
cbp_table[left_xy[
LBOT]] & 0x8000) >> 12;
4088 nnz_cache[
scan8[0]] =
4089 nnz_cache[scan8[1]] =
4090 nnz_cache[scan8[2]] =
4091 nnz_cache[scan8[3]] = (h->
cbp & 0x1000) >> 12;
4093 nnz_cache[scan8[0 + 4]] =
4094 nnz_cache[scan8[1 + 4]] =
4095 nnz_cache[scan8[2 + 4]] =
4096 nnz_cache[scan8[3 + 4]] = (h->
cbp & 0x2000) >> 12;
4098 nnz_cache[scan8[0 + 8]] =
4099 nnz_cache[scan8[1 + 8]] =
4100 nnz_cache[scan8[2 + 8]] =
4101 nnz_cache[scan8[3 + 8]] = (h->
cbp & 0x4000) >> 12;
4103 nnz_cache[scan8[0 + 12]] =
4104 nnz_cache[scan8[1 + 12]] =
4105 nnz_cache[scan8[2 + 12]] =
4106 nnz_cache[scan8[3 + 12]] = (h->
cbp & 0x8000) >> 12;
4115 uint8_t *dest_y, *dest_cb, *dest_cr;
4116 int linesize, uvlinesize, mb_x, mb_y;
4123 for (mb_x = start_x; mb_x < end_x; mb_x++)
4124 for (mb_y = end_mb_y -
FRAME_MBAFF; mb_y <= end_mb_y; mb_y++) {
4138 ((mb_x << pixel_shift) + mb_y * h->
linesize) * 16;
4140 (mb_x << pixel_shift) * (8 <<
CHROMA444) +
4143 (mb_x << pixel_shift) * (8 <<
CHROMA444) +
4168 linesize, uvlinesize);
4171 dest_cr, linesize, uvlinesize);
4203 if ((top + height) >= pic_height)
4204 height += deblock_border;
4205 top -= deblock_border;
4208 if (top >= pic_height || (top + height) < 0)
4211 height =
FFMIN(height, pic_height - top);
4227 int endx,
int endy,
int status)
4239 int lf_x_start = h->
mb_x;
4285 if (h->
mb_x >= lf_x_start)
4293 "error while decoding MB %d %d, bytestream (%td)\n",
4303 h->
mb_x = lf_x_start = 0;
4318 if (h->
mb_x > lf_x_start)
4342 "error while decoding MB %d %d\n", h->
mb_x, h->
mb_y);
4350 h->
mb_x = lf_x_start = 0;
4386 if (h->
mb_x > lf_x_start)
4416 if (context_count == 1) {
4420 for (i = 1; i < context_count; i++) {
4429 NULL, context_count,
sizeof(
void *));
4438 for (i = 1; i < context_count; i++)
4447 int parse_extradata)
4455 int nals_needed = 0;
4458 int first_slice = 0;
4473 if (buf_size > 8 &&
AV_RB32(buf) == 1 &&
AV_RB32(buf+5) > (
unsigned)buf_size) {
4475 }
else if(buf_size > 3 &&
AV_RB32(buf) > 1 &&
AV_RB32(buf) <= (
unsigned)buf_size)
4479 for (; pass <= 1; pass++) {
4482 next_avc = h->
is_avc ? 0 : buf_size;
4492 if (buf_index >= next_avc) {
4497 nalsize = (nalsize << 8) | buf[buf_index++];
4498 if (nalsize <= 0 || nalsize > buf_size - buf_index) {
4500 "AVC: nal size %d\n", nalsize);
4503 next_avc = buf_index + nalsize;
4506 for (; buf_index + 3 < next_avc; buf_index++)
4508 if (buf[buf_index] == 0 &&
4509 buf[buf_index + 1] == 0 &&
4510 buf[buf_index + 2] == 1)
4513 if (buf_index + 3 >= buf_size) {
4514 buf_index = buf_size;
4519 if (buf_index >= next_avc)
4526 &consumed, next_avc - buf_index);
4527 if (ptr ==
NULL || dst_length < 0) {
4531 i = buf_index + consumed;
4533 buf[i] == 0x00 && buf[i + 1] == 0x00 &&
4534 buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
4538 while(dst_length > 0 && ptr[dst_length - 1] == 0)
4540 bit_length = !dst_length ? 0
4547 if (h->
is_avc && (nalsize != consumed) && nalsize)
4549 "AVC: Consumed only %d bytes instead of %d\n",
4552 buf_index += consumed;
4563 nals_needed = nal_index;
4570 nals_needed = nal_index;
4593 if (parse_extradata) {
4612 "Invalid mix of idr and non-idr slices\n");
4675 &buf[buf_index - consumed],
4680 static const uint8_t start_code[] = {
4683 sizeof(start_code));
4731 "SPS decoding failure, trying again with the complete NAL\n");
4733 av_assert0(next_avc - buf_index + consumed == nalsize);
4734 if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
4737 8*(next_avc - buf_index + consumed - 1));
4767 else if (err == 1) {
4800 if (pos + 10 > buf_size)
4810 int buf_size = avpkt->
size;
4820 if (buf_size == 0) {
4850 if(h->
is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
4851 int cnt= buf[5]&0x1f;
4855 if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
4864 if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
4884 buf_size >= 4 && !memcmp(
"Q264", buf, 4))
4905 assert(pict->
data[0] || !*got_frame);
5002 #if CONFIG_H264_VDPAU_DECODER
5003 AVCodec ff_h264_vdpau_decoder = {
5004 .
name =
"h264_vdpau",
5013 .long_name =
NULL_IF_CONFIG_SMALL(
"H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),