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, 30, 40, 38 },
157 { 36, 32, 58, 34, 46, 43 },
161 #if CONFIG_H264_DXVA2_HWACCEL
164 #if CONFIG_H264_VAAPI_HWACCEL
167 #if CONFIG_H264_VDA_HWACCEL
170 #if CONFIG_H264_VDPAU_HWACCEL
178 #if CONFIG_H264_DXVA2_HWACCEL
181 #if CONFIG_H264_VAAPI_HWACCEL
184 #if CONFIG_H264_VDA_HWACCEL
187 #if CONFIG_H264_VDPAU_HWACCEL
202 int mb_x,
int mb_y,
int mb_intra,
int mb_skipped)
268 offset[2] = (y >> vshift) * src->
linesize[1];
292 for (i = 0; i < 2; i++) {
297 memset((
uint8_t*)pic + off, 0,
sizeof(*pic) - off);
333 for (i = 0; i < 2; i++) {
349 for (i = 0; i < 2; i++)
404 const int b4_stride = h->
mb_width * 4 + 1;
405 const int b4_array_size = b4_stride * h->
mb_height * 4;
470 for (i = 0; i < 2; i++) {
483 return (ret < 0) ? ret :
AVERROR(ENOMEM);
488 if (pic->
f.
data[0] == NULL)
503 if (i == MAX_PICTURE_COUNT)
520 static const int8_t top[12] = {
523 static const int8_t left[12] = {
529 for (i = 0; i < 4; i++) {
533 "top block unavailable for requested intra4x4 mode %d at %d %d\n",
543 static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
544 for (i = 0; i < 4; i++)
549 "left block unavailable for requested intra4x4 mode %d at %d %d\n",
572 "out of range intra chroma pred mode at %d %d\n",
581 "top block unavailable for requested intra mode at %d %d\n",
597 "left block unavailable for requested intra mode at %d %d\n",
607 int *dst_length,
int *consumed,
int length)
620 #define STARTCODE_TEST \
621 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
622 if (src[i + 2] != 3) { \
629 #if HAVE_FAST_UNALIGNED
630 #define FIND_FIRST_ZERO \
631 if (i > 0 && !src[i]) \
637 for (i = 0; i + 1 <
length; i += 9) {
639 (
AV_RN64A(src + i) - 0x0100010001000101ULL)) &
640 0x8000800080008080ULL))
647 for (i = 0; i + 1 <
length; i += 5) {
649 (
AV_RN32A(src + i) - 0x01000101U)) &
658 for (i = 0; i + 1 <
length; i += 2) {
661 if (i > 0 && src[i - 1] == 0)
683 memcpy(dst, src, length);
690 while (si + 2 < length) {
692 if (src[si + 2] > 3) {
693 dst[di++] = src[si++];
694 dst[di++] = src[si++];
695 }
else if (src[si] == 0 && src[si + 1] == 0) {
696 if (src[si + 2] == 3) {
705 dst[di++] = src[si++];
708 dst[di++] = src[si++];
731 for (r = 1; r < 9; r++) {
740 int height,
int y_offset,
int list)
743 int filter_height_down = (raw_my & 3) ? 3 : 0;
744 int full_my = (raw_my >> 2) + y_offset;
745 int bottom = full_my + filter_height_down +
height;
749 return FFMAX(0, bottom);
753 int height,
int y_offset,
int list0,
754 int list1,
int *nrefs)
770 if (refs[0][ref_n] < 0)
772 refs[0][ref_n] =
FFMAX(refs[0][ref_n], my);
783 if (refs[1][ref_n] < 0)
785 refs[1][ref_n] =
FFMAX(refs[1][ref_n], my);
797 const int mb_xy = h->
mb_xy;
800 int nrefs[2] = { 0 };
803 memset(refs, -1,
sizeof(refs));
823 for (i = 0; i < 4; i++) {
826 int y_offset = (i & 2) << 2;
830 IS_DIR(sub_mb_type, 0, 0),
831 IS_DIR(sub_mb_type, 0, 1),
835 IS_DIR(sub_mb_type, 0, 0),
836 IS_DIR(sub_mb_type, 0, 1),
839 IS_DIR(sub_mb_type, 0, 0),
840 IS_DIR(sub_mb_type, 0, 1),
844 IS_DIR(sub_mb_type, 0, 0),
845 IS_DIR(sub_mb_type, 0, 1),
848 IS_DIR(sub_mb_type, 0, 0),
849 IS_DIR(sub_mb_type, 0, 1),
854 for (j = 0; j < 4; j++) {
855 int sub_y_offset = y_offset + 2 * (j & 2);
857 IS_DIR(sub_mb_type, 0, 0),
858 IS_DIR(sub_mb_type, 0, 1),
865 for (list = h->
list_count - 1; list >= 0; list--)
866 for (ref = 0; ref < 48 && nrefs[list]; ref++) {
867 int row = refs[list][ref];
872 int pic_height = 16 * h->
mb_height >> ref_field_picture;
879 FFMIN((row >> 1) - !(row & 1),
883 FFMIN((row >> 1), pic_height - 1),
887 FFMIN(row * 2 + ref_field,
892 FFMIN(row, pic_height - 1),
896 FFMIN(row, pic_height - 1),
908 int src_x_offset,
int src_y_offset,
911 int pixel_shift,
int chroma_idc)
915 const int luma_xy = (mx & 3) + ((my & 3) << 2);
920 int extra_height = 0;
922 const int full_mx = mx >> 2;
923 const int full_my = my >> 2;
924 const int pic_width = 16 * h->
mb_width;
933 if (full_mx < 0 - extra_width ||
934 full_my < 0 - extra_height ||
935 full_mx + 16 > pic_width + extra_width ||
936 full_my + 16 > pic_height + extra_height) {
940 16 + 5, 16 + 5 , full_mx - 2,
941 full_my - 2, pic_width, pic_height);
953 if (chroma_idc == 3 ) {
960 full_mx - 2, full_my - 2,
961 pic_width, pic_height);
974 full_mx - 2, full_my - 2,
975 pic_width, pic_height);
984 ysh = 3 - (chroma_idc == 2 );
985 if (chroma_idc == 1 &&
MB_FIELD(h)) {
988 emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
991 src_cb = pic->
f.
data[1] + ((mx >> 3) << pixel_shift) +
993 src_cr = pic->
f.
data[2] + ((mx >> 3) << pixel_shift) +
998 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
999 pic_width >> 1, pic_height >> (chroma_idc == 1 ));
1003 height >> (chroma_idc == 1 ),
1004 mx & 7, (my << (chroma_idc == 2 )) & 7);
1008 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
1009 pic_width >> 1, pic_height >> (chroma_idc == 1 ));
1012 chroma_op(dest_cr, src_cr, h->
mb_uvlinesize, height >> (chroma_idc == 1 ),
1013 mx & 7, (my << (chroma_idc == 2 )) & 7);
1020 int x_offset,
int y_offset,
1025 int list0,
int list1,
1026 int pixel_shift,
int chroma_idc)
1031 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
1032 if (chroma_idc == 3 ) {
1033 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
1034 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
1035 }
else if (chroma_idc == 2 ) {
1036 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
1037 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
1039 dest_cb += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
1040 dest_cr += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
1042 x_offset += 8 * h->
mb_x;
1048 dest_y, dest_cb, dest_cr, x_offset, y_offset,
1049 qpix_op, chroma_op, pixel_shift, chroma_idc);
1052 chroma_op = chroma_avg;
1058 dest_y, dest_cb, dest_cr, x_offset, y_offset,
1059 qpix_op, chroma_op, pixel_shift, chroma_idc);
1067 int x_offset,
int y_offset,
1074 int list0,
int list1,
1075 int pixel_shift,
int chroma_idc)
1079 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
1080 if (chroma_idc == 3 ) {
1082 chroma_weight_avg = luma_weight_avg;
1083 chroma_weight_op = luma_weight_op;
1084 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
1085 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
1086 }
else if (chroma_idc == 2 ) {
1088 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
1089 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
1091 chroma_height = height >> 1;
1092 dest_cb += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
1093 dest_cr += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
1095 x_offset += 8 * h->
mb_x;
1098 if (list0 && list1) {
1108 dest_y, dest_cb, dest_cr,
1109 x_offset, y_offset, qpix_put, chroma_put,
1110 pixel_shift, chroma_idc);
1112 tmp_y, tmp_cb, tmp_cr,
1113 x_offset, y_offset, qpix_put, chroma_put,
1114 pixel_shift, chroma_idc);
1118 int weight1 = 64 - weight0;
1120 height, 5, weight0, weight1, 0);
1122 chroma_height, 5, weight0, weight1, 0);
1124 chroma_height, 5, weight0, weight1, 0);
1126 luma_weight_avg(dest_y, tmp_y, h->
mb_linesize, height,
1132 chroma_weight_avg(dest_cb, tmp_cb, h->
mb_uvlinesize, chroma_height,
1138 chroma_weight_avg(dest_cr, tmp_cr, h->
mb_uvlinesize, chroma_height,
1146 int list = list1 ? 1 : 0;
1149 mc_dir_part(h, ref, n, square, height, delta, list,
1150 dest_y, dest_cb, dest_cr, x_offset, y_offset,
1151 qpix_put, chroma_put, pixel_shift, chroma_idc);
1171 int pixel_shift,
int chroma_idc)
1180 int off = (mx << pixel_shift) +
1182 (64 << pixel_shift);
1184 if (chroma_idc == 3 ) {
1188 off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (h->
mb_x&7))*h->
uvlinesize;
1213 for (i = 0; i < 3; i++)
1221 if (free_rbsp && h->
DPB) {
1225 }
else if (h->
DPB) {
1264 for (i = 0; i < 6; i++) {
1266 for (j = 0; j < i; j++)
1275 for (q = 0; q < max_qp + 1; q++) {
1278 for (x = 0; x < 64; x++)
1290 for (i = 0; i < 6; i++) {
1292 for (j = 0; j < i; j++)
1301 for (q = 0; q < max_qp + 1; q++) {
1304 for (x = 0; x < 16; x++)
1319 for (i = 0; i < 6; i++)
1320 for (x = 0; x < 16; x++)
1323 for (i = 0; i < 6; i++)
1324 for (x = 0; x < 64; x++)
1336 row_mb_num * 8 *
sizeof(
uint8_t), fail)
1338 big_mb_num * 48 *
sizeof(
uint8_t), fail)
1342 big_mb_num *
sizeof(uint16_t), fail)
1344 big_mb_num *
sizeof(
uint8_t), fail)
1346 16 * row_mb_num *
sizeof(
uint8_t), fail);
1348 16 * row_mb_num *
sizeof(
uint8_t), fail);
1350 4 * big_mb_num *
sizeof(
uint8_t), fail);
1352 big_mb_num *
sizeof(
uint8_t), fail)
1359 big_mb_num *
sizeof(uint32_t), fail);
1361 big_mb_num *
sizeof(uint32_t), fail);
1363 for (x = 0; x < h->
mb_width; x++) {
1365 const int b_xy = 4 * x + 4 * y * h->
b_stride;
1426 int yc_size = y_size + 2 * c_size;
1441 if (CONFIG_ERROR_RESILIENCE) {
1465 mb_array_size *
sizeof(
uint8_t), fail);
1479 for (i = 0; i < yc_size; i++)
1490 int parse_extradata);
1497 if (!buf || size <= 0)
1501 int i, cnt, nalsize;
1502 const unsigned char *p =
buf;
1514 cnt = *(p + 5) & 0x1f;
1516 for (i = 0; i < cnt; i++) {
1518 if(nalsize > size - (p-buf))
1523 "Decoding sps %d from avcC failed\n", i);
1530 for (i = 0; i < cnt; i++) {
1532 if(nalsize > size - (p-buf))
1537 "Decoding pps %d from avcC failed\n", i);
1577 if (CONFIG_ERROR_RESILIENCE)
1643 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
1644 #undef REBASE_PICTURE
1645 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
1646 ((pic && pic >= old_ctx->DPB && \
1647 pic < old_ctx->DPB + MAX_PICTURE_COUNT) ? \
1648 &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
1656 for (i = 0; i <
count; i++) {
1657 assert((
IN_RANGE(from[i], old_base,
sizeof(*old_base)) ||
1669 for (i = 0; i <
count; i++) {
1670 if (to[i] && !from[i])
1672 else if (from[i] && !to[i])
1676 memcpy(to[i], from[i], size);
1698 #define copy_fields(to, from, start_field, end_field) \
1699 memcpy(&to->start_field, &from->start_field, \
1700 (char *)&to->end_field - (char *)&to->start_field)
1711 int context_reinitialized = 0;
1718 (h->
width != h1->width ||
1719 h->
height != h1->height ||
1733 h->
width = h1->width;
1752 context_reinitialized = 1;
1777 memcpy(h, h1, offsetof(
H264Context, intra_pcm_ptr));
1778 memcpy(&h->
cabac, &h1->cabac,
1785 memset(&h->
er, 0,
sizeof(h->
er));
1786 memset(&h->
me, 0,
sizeof(h->
me));
1787 memset(&h->
mb, 0,
sizeof(h->
mb));
1797 for (i = 0; i < 2; i++) {
1802 if (h1->context_initialized) {
1842 if (h1->DPB && h1->DPB[i].f.data[0] &&
1869 copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1871 for (i = 0; i < 6; i++)
1873 (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1875 for (i = 0; i < 6; i++)
1877 (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1894 if (context_reinitialized)
1960 if (CONFIG_ERROR_RESILIENCE) {
1967 if (CONFIG_ERROR_RESILIENCE) {
1975 for (i = 0; i < 16; i++) {
1979 for (i = 0; i < 16; i++) {
2016 int i, pics, out_of_order, out_idx;
2116 for (i = 0; 1; i++) {
2128 out_of_order =
FFMAX(out_of_order, 1);
2193 int linesize,
int uvlinesize,
2203 src_cb -= uvlinesize;
2204 src_cr -= uvlinesize;
2210 AV_COPY128(top_border, src_y + 15 * linesize);
2212 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
2216 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
2217 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
2218 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
2219 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
2221 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
2222 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
2224 }
else if (chroma422) {
2226 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
2227 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
2229 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
2230 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
2234 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
2235 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
2237 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
2238 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
2252 AV_COPY128(top_border, src_y + 16 * linesize);
2254 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
2259 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
2260 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
2261 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
2262 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
2264 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
2265 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
2267 }
else if (chroma422) {
2269 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
2270 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
2272 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
2273 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
2277 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
2278 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
2280 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
2281 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
2289 int linesize,
int uvlinesize,
2290 int xchg,
int chroma444,
2291 int simple,
int pixel_shift)
2293 int deblock_topleft;
2312 deblock_topleft = (h->
mb_x > 0);
2316 src_y -= linesize + 1 + pixel_shift;
2317 src_cb -= uvlinesize + 1 + pixel_shift;
2318 src_cr -= uvlinesize + 1 + pixel_shift;
2323 #define XCHG(a, b, xchg) \
2324 if (pixel_shift) { \
2326 AV_SWAP64(b + 0, a + 0); \
2327 AV_SWAP64(b + 8, a + 8); \
2337 if (deblock_topleft) {
2338 XCHG(top_border_m1 + (8 << pixel_shift),
2339 src_y - (7 << pixel_shift), 1);
2341 XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
2342 XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
2345 src_y + (17 << pixel_shift), 1);
2349 if (deblock_topleft) {
2350 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2351 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2353 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
2354 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
2355 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
2356 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
2358 XCHG(h->
top_borders[top_idx][h->
mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
2359 XCHG(h->
top_borders[top_idx][h->
mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
2362 if (deblock_topleft) {
2363 XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
2364 XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
2366 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
2367 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
2376 if (high_bit_depth) {
2385 if (high_bit_depth) {
2392 int mb_type,
int is_h264,
2394 int transform_bypass,
2404 block_offset += 16 * p;
2407 if (transform_bypass) {
2414 for (i = 0; i < 16; i += 4) {
2415 uint8_t *
const ptr = dest_y + block_offset[i];
2418 h->
hpc.
pred8x8l_add[dir](ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2424 if (nnz == 1 &&
dctcoef_get(h->
mb, pixel_shift, i * 16 + p * 256))
2425 idct_dc_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2427 idct_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2432 if (transform_bypass) {
2439 for (i = 0; i < 16; i++) {
2440 uint8_t *
const ptr = dest_y + block_offset[i];
2444 h->
hpc.
pred4x4_add[dir](ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2452 if (!topright_avail) {
2454 tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
2455 topright = (
uint8_t *)&tr_high;
2457 tr = ptr[3 - linesize] * 0x01010101
u;
2461 topright = ptr + (4 << pixel_shift) - linesize;
2465 h->
hpc.
pred4x4[dir](ptr, topright, linesize);
2469 if (nnz == 1 &&
dctcoef_get(h->
mb, pixel_shift, i * 16 + p * 256))
2470 idct_dc_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2472 idct_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
2473 }
else if (CONFIG_SVQ3_DECODER)
2483 if (!transform_bypass)
2488 static const uint8_t dc_mapping[16] = {
2489 0 * 16, 1 * 16, 4 * 16, 5 * 16,
2490 2 * 16, 3 * 16, 6 * 16, 7 * 16,
2491 8 * 16, 9 * 16, 12 * 16, 13 * 16,
2492 10 * 16, 11 * 16, 14 * 16, 15 * 16
2494 for (i = 0; i < 16; i++)
2496 pixel_shift, dc_mapping[i],
2501 }
else if (CONFIG_SVQ3_DECODER)
2508 int is_h264,
int simple,
2509 int transform_bypass,
2517 block_offset += 16 * p;
2521 if (transform_bypass) {
2526 h->
mb + (p * 256 << pixel_shift),
2529 for (i = 0; i < 16; i++)
2533 h->
mb + (i * 16 + p * 256 << pixel_shift),
2538 h->
mb + (p * 256 << pixel_shift),
2542 }
else if (h->
cbp & 15) {
2543 if (transform_bypass) {
2544 const int di =
IS_8x8DCT(mb_type) ? 4 : 1;
2547 for (i = 0; i < 16; i += di)
2550 h->
mb + (i * 16 + p * 256 << pixel_shift),
2555 h->
mb + (p * 256 << pixel_shift),
2560 h->
mb + (p * 256 << pixel_shift),
2565 }
else if (CONFIG_SVQ3_DECODER) {
2566 for (i = 0; i < 16; i++)
2569 uint8_t *
const ptr = dest_y + block_offset[i];
2591 const int mb_xy = h->
mb_xy;
2593 int is_complex = CONFIG_SMALL || h->
is_complex ||
2598 hl_decode_mb_444_complex(h);
2600 hl_decode_mb_444_simple_8(h);
2601 }
else if (is_complex) {
2602 hl_decode_mb_complex(h);
2604 hl_decode_mb_simple_16(h);
2606 hl_decode_mb_simple_8(h);
2612 int luma_def, chroma_def;
2622 for (list = 0; list < 2; list++) {
2625 for (i = 0; i < h->
ref_count[list]; i++) {
2626 int luma_weight_flag, chroma_weight_flag;
2629 if (luma_weight_flag) {
2644 if (chroma_weight_flag) {
2646 for (j = 0; j < 2; j++) {
2657 for (j = 0; j < 2; j++) {
2678 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
2680 for (i = 0; i < 2; i++) {
2712 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
2714 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
2718 int td = av_clip(poc1 - poc0, -128, 127);
2720 int tb = av_clip(cur_poc - poc0, -128, 127);
2721 int tx = (16384 + (
FFABS(td) >> 1)) /
td;
2722 int dist_scale_factor = (tb * tx + 32) >> 8;
2723 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
2724 w = 64 - dist_scale_factor;
2837 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
2848 expected_delta_per_poc_cycle = 0;
2853 if (abs_frame_num > 0) {
2857 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
2858 for (i = 0; i <= frame_num_in_poc_cycle; i++)
2866 field_poc[0] = expectedpoc + h->
delta_poc[0];
2882 pic_field_poc[0] = field_poc[0];
2884 pic_field_poc[1] = field_poc[1];
2885 *pic_poc =
FFMIN(pic_field_poc[0], pic_field_poc[1]);
2896 for (i = 0; i < 16; i++) {
2897 #define T(x) (x >> 2) | ((x << 2) & 0xF)
2902 for (i = 0; i < 64; i++) {
2903 #define T(x) (x >> 3) | ((x & 7) << 3)
2933 if (CONFIG_H264_VDPAU_DECODER &&
2951 "hardware accelerator failed to decode picture\n");
2954 if (CONFIG_H264_VDPAU_DECODER &&
2970 if (CONFIG_ERROR_RESILIENCE &&
3047 "Reenabling low delay requires a codec flush.\n");
3057 "Different chroma and luma bit depth");
3067 "VDPAU decoding does not support video colorspace.\n");
3083 if (CONFIG_ERROR_RESILIENCE)
3192 if (width <= 0 || height <= 0) {
3216 int nb_slices = (HAVE_THREADS &&
3245 "Could not allocate memory for h264\n");
3256 " reducing to %d\n", nb_slices, max_slices);
3257 nb_slices = max_slices;
3272 if (CONFIG_ERROR_RESILIENCE) {
3320 int num_ref_idx_active_override_flag;
3332 num_ref_idx_active_override_flag =
get_bits1(&h->
gb);
3334 if (num_ref_idx_active_override_flag) {
3373 unsigned int first_mb_in_slice;
3374 unsigned int pps_id;
3376 unsigned int slice_type, tmp, i, j;
3377 int last_pic_structure, last_pic_droppable;
3379 int needs_reinit = 0;
3380 int field_pic_flag, bottom_field_flag;
3387 if (first_mb_in_slice == 0) {
3403 if (slice_type > 9) {
3405 "slice type too large (%d) at %d %d\n",
3409 if (slice_type > 4) {
3429 "non-existing PPS %u referenced\n",
3437 "non-existing SPS %u referenced\n",
3532 "h264_slice_header_init() failed\n");
3539 "Cannot (re-)initialize context during parallel decoding.\n");
3549 "h264_slice_header_init() failed\n");
3574 if (field_pic_flag) {
3588 "Changing field mode (%d -> %d) between slices is not allowed\n",
3595 "unset cur_pic_ptr on %d. slice\n",
3606 if (unwrap_prev_frame_num > h->
frame_num)
3607 unwrap_prev_frame_num -= max_frame_num;
3611 if (unwrap_prev_frame_num < 0)
3612 unwrap_prev_frame_num += max_frame_num;
3659 "Invalid field mode combination %d/%d\n",
3664 }
else if (last_pic_droppable != h->
droppable) {
3666 "Found reference and non-reference fields in the same frame, which");
3787 first_mb_in_slice >= h->
mb_num) {
3857 for (i = 0; i < 2; i++) {
3921 "deblocking_filter_idc %u out of range\n", tmp);
3934 "deblocking filter parameters %d %d out of range\n",
3959 "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
3964 "Deblocking switched inside frame.\n");
3989 for (j = 0; j < 2; j++) {
3992 for (i = 0; i < 16; i++) {
3994 if (j < h->list_count && i < h->ref_count[j] &&
4013 for (i = 0; i < 16; i++)
4016 ref2frm[18 + 1] = -1;
4017 for (i = 16; i < 48; i++)
4018 ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
4028 "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",
4069 int mb_type,
int top_xy,
4072 int left_type[LEFT_MBS],
4073 int mb_xy,
int list)
4080 const int b_xy = h->
mb2b_xy[top_xy] + 3 * b_stride;
4081 const int b8_xy = 4 * top_xy + 2;
4084 ref_cache[0 - 1 * 8] =
4085 ref_cache[1 - 1 * 8] = ref2frm[list][h->
cur_pic.
ref_index[list][b8_xy + 0]];
4086 ref_cache[2 - 1 * 8] =
4087 ref_cache[3 - 1 * 8] = ref2frm[list][h->
cur_pic.
ref_index[list][b8_xy + 1]];
4096 const int b8_xy = 4 * left_xy[
LTOP] + 1;
4103 ref_cache[-1 + 8] = ref2frm[list][h->
cur_pic.
ref_index[list][b8_xy + 2 * 0]];
4104 ref_cache[-1 + 16] =
4105 ref_cache[-1 + 24] = ref2frm[list][h->
cur_pic.
ref_index[list][b8_xy + 2 * 1]];
4113 ref_cache[-1 + 16] =
4131 uint32_t ref01 = (
pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
4132 uint32_t ref23 = (
pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
4133 AV_WN32A(&ref_cache[0 * 8], ref01);
4134 AV_WN32A(&ref_cache[1 * 8], ref01);
4135 AV_WN32A(&ref_cache[2 * 8], ref23);
4136 AV_WN32A(&ref_cache[3 * 8], ref23);
4141 AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
4142 AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
4143 AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
4144 AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
4154 const int mb_xy = h->
mb_xy;
4165 left_xy[
LBOT] = left_xy[
LTOP] = mb_xy - 1;
4170 if (left_mb_field_flag != curr_mb_field_flag)
4173 if (curr_mb_field_flag)
4176 if (left_mb_field_flag != curr_mb_field_flag)
4190 if (qp <= qp_thresh &&
4191 (left_xy[
LTOP] < 0 ||
4197 if ((left_xy[
LTOP] < 0 ||
4212 left_type[
LTOP] = left_type[
LBOT] = 0;
4217 left_type[
LTOP] = left_type[
LBOT] = 0;
4227 top_type, left_type, mb_xy, 0);
4230 top_type, left_type, mb_xy, 1);
4234 AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
4235 AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
4236 AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
4237 AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
4242 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
4245 if (left_type[
LTOP]) {
4247 nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
4248 nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
4249 nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
4250 nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
4257 nnz_cache[4 + 8 * 0] =
4258 nnz_cache[5 + 8 * 0] = (h->
cbp_table[top_xy] & 0x4000) >> 12;
4259 nnz_cache[6 + 8 * 0] =
4260 nnz_cache[7 + 8 * 0] = (h->
cbp_table[top_xy] & 0x8000) >> 12;
4263 nnz_cache[3 + 8 * 1] =
4264 nnz_cache[3 + 8 * 2] = (h->
cbp_table[left_xy[
LTOP]] & 0x2000) >> 12;
4267 nnz_cache[3 + 8 * 3] =
4268 nnz_cache[3 + 8 * 4] = (h->
cbp_table[left_xy[
LBOT]] & 0x8000) >> 12;
4272 nnz_cache[
scan8[0]] =
4273 nnz_cache[scan8[1]] =
4274 nnz_cache[scan8[2]] =
4275 nnz_cache[scan8[3]] = (h->
cbp & 0x1000) >> 12;
4277 nnz_cache[scan8[0 + 4]] =
4278 nnz_cache[scan8[1 + 4]] =
4279 nnz_cache[scan8[2 + 4]] =
4280 nnz_cache[scan8[3 + 4]] = (h->
cbp & 0x2000) >> 12;
4282 nnz_cache[scan8[0 + 8]] =
4283 nnz_cache[scan8[1 + 8]] =
4284 nnz_cache[scan8[2 + 8]] =
4285 nnz_cache[scan8[3 + 8]] = (h->
cbp & 0x4000) >> 12;
4287 nnz_cache[scan8[0 + 12]] =
4288 nnz_cache[scan8[1 + 12]] =
4289 nnz_cache[scan8[2 + 12]] =
4290 nnz_cache[scan8[3 + 12]] = (h->
cbp & 0x8000) >> 12;
4299 uint8_t *dest_y, *dest_cb, *dest_cr;
4300 int linesize, uvlinesize, mb_x, mb_y;
4307 for (mb_x = start_x; mb_x < end_x; mb_x++)
4308 for (mb_y = end_mb_y -
FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
4322 ((mb_x << pixel_shift) + mb_y * h->
linesize) * 16;
4324 (mb_x << pixel_shift) * (8 <<
CHROMA444(h)) +
4327 (mb_x << pixel_shift) * (8 <<
CHROMA444(h)) +
4352 linesize, uvlinesize);
4355 dest_cr, linesize, uvlinesize);
4387 if ((top + height) >= pic_height)
4388 height += deblock_border;
4389 top -= deblock_border;
4392 if (top >= pic_height || (top + height) < 0)
4395 height =
FFMIN(height, pic_height - top);
4411 int endx,
int endy,
int status)
4413 if (CONFIG_ERROR_RESILIENCE) {
4423 int lf_x_start = h->
mb_x;
4479 if (h->
mb_x >= lf_x_start)
4487 "error while decoding MB %d %d, bytestream (%td)\n",
4497 h->
mb_x = lf_x_start = 0;
4512 if (h->
mb_x > lf_x_start)
4536 "error while decoding MB %d %d\n", h->
mb_x, h->
mb_y);
4544 h->
mb_x = lf_x_start = 0;
4581 if (h->
mb_x > lf_x_start)
4613 if (context_count == 1) {
4617 for (i = 1; i < context_count; i++) {
4619 if (CONFIG_ERROR_RESILIENCE) {
4626 NULL, context_count,
sizeof(
void *));
4634 if (CONFIG_ERROR_RESILIENCE) {
4635 for (i = 1; i < context_count; i++)
4646 int parse_extradata)
4654 int nals_needed = 0;
4657 int first_slice = 0;
4673 if (buf_size > 8 &&
AV_RB32(buf) == 1 &&
AV_RB32(buf+5) > (
unsigned)buf_size) {
4675 }
else if(buf_size > 3 &&
AV_RB32(buf) > 1 &&
AV_RB32(buf) <= (
unsigned)buf_size)
4679 for (; pass <= 1; pass++) {
4682 next_avc = h->
is_avc ? 0 : buf_size;
4692 if (buf_index >= next_avc) {
4697 nalsize = (nalsize << 8) | buf[buf_index++];
4698 if (nalsize <= 0 || nalsize > buf_size - buf_index) {
4700 "AVC: nal size %d\n", nalsize);
4703 next_avc = buf_index + nalsize;
4706 for (; buf_index + 3 < next_avc; buf_index++)
4708 if (buf[buf_index] == 0 &&
4709 buf[buf_index + 1] == 0 &&
4710 buf[buf_index + 2] == 1)
4713 if (buf_index + 3 >= buf_size) {
4714 buf_index = buf_size;
4719 if (buf_index >= next_avc)
4726 &consumed, next_avc - buf_index);
4727 if (ptr == NULL || dst_length < 0) {
4731 i = buf_index + consumed;
4733 buf[i] == 0x00 && buf[i + 1] == 0x00 &&
4734 buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
4738 while(dst_length > 0 && ptr[dst_length - 1] == 0)
4740 bit_length = !dst_length ? 0
4747 if (h->
is_avc && (nalsize != consumed) && nalsize)
4749 "AVC: Consumed only %d bytes instead of %d\n",
4752 buf_index += consumed;
4763 nals_needed = nal_index;
4770 nals_needed = nal_index;
4794 if (parse_extradata) {
4802 "Ignoring NAL %d in global header/extradata\n",
4816 "Invalid mix of idr and non-idr slices\n");
4864 if (CONFIG_H264_VDPAU_DECODER &&
4879 &buf[buf_index - consumed],
4883 }
else if (CONFIG_H264_VDPAU_DECODER &&
4887 sizeof(start_code));
4889 &buf[buf_index - consumed],
4936 "SPS decoding failure, trying again with the complete NAL\n");
4938 av_assert0(next_avc - buf_index + consumed == nalsize);
4939 if ((next_avc - buf_index + consumed - 1) >= INT_MAX/8)
4942 8*(next_avc - buf_index + consumed - 1));
4972 else if (err == 1) {
4994 return (ret < 0) ? ret : buf_index;
5004 if (pos + 10 > buf_size)
5023 for (i = 0; i < 3; i++) {
5028 dst->
data[i] += off;
5037 int buf_size = avpkt->
size;
5048 if (buf_size == 0) {
5080 if(h->
is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
5081 int cnt= buf[5]&0x1f;
5085 if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
5094 if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
5114 buf_size >= 4 && !memcmp(
"Q264", buf, 4))
5134 if (CONFIG_MPEGVIDEO) {
5142 assert(pict->
data[0] || !*got_frame);
5228 #if CONFIG_H264_VDPAU_DECODER
5229 AVCodec ff_h264_vdpau_decoder = {
5230 .
name =
"h264_vdpau",
5231 .long_name =
NULL_IF_CONFIG_SMALL(
"H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),