00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #define UNCHECKED_BITSTREAM_READER 1
00029
00030 #include "libavutil/imgutils.h"
00031 #include "libavutil/opt.h"
00032 #include "internal.h"
00033 #include "cabac.h"
00034 #include "cabac_functions.h"
00035 #include "dsputil.h"
00036 #include "avcodec.h"
00037 #include "mpegvideo.h"
00038 #include "h264.h"
00039 #include "h264data.h"
00040 #include "h264_mvpred.h"
00041 #include "golomb.h"
00042 #include "mathops.h"
00043 #include "rectangle.h"
00044 #include "thread.h"
00045 #include "vdpau_internal.h"
00046 #include "libavutil/avassert.h"
00047
00048
00049 #include <assert.h>
00050
00051 const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
00052
00053 static const uint8_t rem6[QP_MAX_NUM + 1] = {
00054 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
00055 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
00056 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
00057 };
00058
00059 static const uint8_t div6[QP_MAX_NUM + 1] = {
00060 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
00061 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
00062 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
00063 };
00064
00065 static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
00066 PIX_FMT_DXVA2_VLD,
00067 PIX_FMT_VAAPI_VLD,
00068 PIX_FMT_VDA_VLD,
00069 PIX_FMT_YUVJ420P,
00070 PIX_FMT_NONE
00071 };
00072
00077 int ff_h264_check_intra4x4_pred_mode(H264Context *h)
00078 {
00079 MpegEncContext *const s = &h->s;
00080 static const int8_t top[12] = {
00081 -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
00082 };
00083 static const int8_t left[12] = {
00084 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
00085 };
00086 int i;
00087
00088 if (!(h->top_samples_available & 0x8000)) {
00089 for (i = 0; i < 4; i++) {
00090 int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
00091 if (status < 0) {
00092 av_log(h->s.avctx, AV_LOG_ERROR,
00093 "top block unavailable for requested intra4x4 mode %d at %d %d\n",
00094 status, s->mb_x, s->mb_y);
00095 return -1;
00096 } else if (status) {
00097 h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
00098 }
00099 }
00100 }
00101
00102 if ((h->left_samples_available & 0x8888) != 0x8888) {
00103 static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
00104 for (i = 0; i < 4; i++)
00105 if (!(h->left_samples_available & mask[i])) {
00106 int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
00107 if (status < 0) {
00108 av_log(h->s.avctx, AV_LOG_ERROR,
00109 "left block unavailable for requested intra4x4 mode %d at %d %d\n",
00110 status, s->mb_x, s->mb_y);
00111 return -1;
00112 } else if (status) {
00113 h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
00114 }
00115 }
00116 }
00117
00118 return 0;
00119 }
00120
00125 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
00126 {
00127 MpegEncContext *const s = &h->s;
00128 static const int8_t top[7] = { LEFT_DC_PRED8x8, 1, -1, -1 };
00129 static const int8_t left[7] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
00130
00131 if (mode > 6U) {
00132 av_log(h->s.avctx, AV_LOG_ERROR,
00133 "out of range intra chroma pred mode at %d %d\n",
00134 s->mb_x, s->mb_y);
00135 return -1;
00136 }
00137
00138 if (!(h->top_samples_available & 0x8000)) {
00139 mode = top[mode];
00140 if (mode < 0) {
00141 av_log(h->s.avctx, AV_LOG_ERROR,
00142 "top block unavailable for requested intra mode at %d %d\n",
00143 s->mb_x, s->mb_y);
00144 return -1;
00145 }
00146 }
00147
00148 if ((h->left_samples_available & 0x8080) != 0x8080) {
00149 mode = left[mode];
00150 if (is_chroma && (h->left_samples_available & 0x8080)) {
00151
00152 mode = ALZHEIMER_DC_L0T_PRED8x8 +
00153 (!(h->left_samples_available & 0x8000)) +
00154 2 * (mode == DC_128_PRED8x8);
00155 }
00156 if (mode < 0) {
00157 av_log(h->s.avctx, AV_LOG_ERROR,
00158 "left block unavailable for requested intra mode at %d %d\n",
00159 s->mb_x, s->mb_y);
00160 return -1;
00161 }
00162 }
00163
00164 return mode;
00165 }
00166
00167 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
00168 int *dst_length, int *consumed, int length)
00169 {
00170 int i, si, di;
00171 uint8_t *dst;
00172 int bufidx;
00173
00174
00175 h->nal_ref_idc = src[0] >> 5;
00176 h->nal_unit_type = src[0] & 0x1F;
00177
00178 src++;
00179 length--;
00180
00181 #if HAVE_FAST_UNALIGNED
00182 #if HAVE_FAST_64BIT
00183 #define RS 7
00184 for (i = 0; i + 1 < length; i += 9) {
00185 if (!((~AV_RN64A(src + i) &
00186 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
00187 0x8000800080008080ULL))
00188 #else
00189 #define RS 3
00190 for (i = 0; i + 1 < length; i += 5) {
00191 if (!((~AV_RN32A(src + i) &
00192 (AV_RN32A(src + i) - 0x01000101U)) &
00193 0x80008080U))
00194 #endif
00195 continue;
00196 if (i > 0 && !src[i])
00197 i--;
00198 while (src[i])
00199 i++;
00200 #else
00201 #define RS 0
00202 for (i = 0; i + 1 < length; i += 2) {
00203 if (src[i])
00204 continue;
00205 if (i > 0 && src[i - 1] == 0)
00206 i--;
00207 #endif
00208 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {
00209 if (src[i + 2] != 3) {
00210
00211 length = i;
00212 }
00213 break;
00214 }
00215 i -= RS;
00216 }
00217
00218
00219 bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
00220
00221 si = h->rbsp_buffer_size[bufidx];
00222 av_fast_padded_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+MAX_MBPAIR_SIZE);
00223 dst = h->rbsp_buffer[bufidx];
00224
00225 if (dst == NULL)
00226 return NULL;
00227
00228 if(i>=length-1){
00229 *dst_length= length;
00230 *consumed= length+1;
00231 if(h->s.avctx->flags2 & CODEC_FLAG2_FAST){
00232 return src;
00233 }else{
00234 memcpy(dst, src, length);
00235 return dst;
00236 }
00237 }
00238
00239
00240 memcpy(dst, src, i);
00241 si = di = i;
00242 while (si + 2 < length) {
00243
00244 if (src[si + 2] > 3) {
00245 dst[di++] = src[si++];
00246 dst[di++] = src[si++];
00247 } else if (src[si] == 0 && src[si + 1] == 0) {
00248 if (src[si + 2] == 3) {
00249 dst[di++] = 0;
00250 dst[di++] = 0;
00251 si += 3;
00252 continue;
00253 } else
00254 goto nsc;
00255 }
00256
00257 dst[di++] = src[si++];
00258 }
00259 while (si < length)
00260 dst[di++] = src[si++];
00261 nsc:
00262
00263 memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00264
00265 *dst_length = di;
00266 *consumed = si + 1;
00267
00268
00269 return dst;
00270 }
00271
00276 static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
00277 {
00278 int v = *src;
00279 int r;
00280
00281 tprintf(h->s.avctx, "rbsp trailing %X\n", v);
00282
00283 for (r = 1; r < 9; r++) {
00284 if (v & 1)
00285 return r;
00286 v >>= 1;
00287 }
00288 return 0;
00289 }
00290
00291 static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
00292 int height, int y_offset, int list)
00293 {
00294 int raw_my = h->mv_cache[list][scan8[n]][1];
00295 int filter_height = (raw_my & 3) ? 2 : 0;
00296 int full_my = (raw_my >> 2) + y_offset;
00297 int top = full_my - filter_height;
00298 int bottom = full_my + filter_height + height;
00299
00300 return FFMAX(abs(top), bottom);
00301 }
00302
00303 static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
00304 int height, int y_offset, int list0,
00305 int list1, int *nrefs)
00306 {
00307 MpegEncContext *const s = &h->s;
00308 int my;
00309
00310 y_offset += 16 * (s->mb_y >> MB_FIELD);
00311
00312 if (list0) {
00313 int ref_n = h->ref_cache[0][scan8[n]];
00314 Picture *ref = &h->ref_list[0][ref_n];
00315
00316
00317
00318
00319 if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
00320 (ref->f.reference & 3) != s->picture_structure) {
00321 my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
00322 if (refs[0][ref_n] < 0)
00323 nrefs[0] += 1;
00324 refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
00325 }
00326 }
00327
00328 if (list1) {
00329 int ref_n = h->ref_cache[1][scan8[n]];
00330 Picture *ref = &h->ref_list[1][ref_n];
00331
00332 if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
00333 (ref->f.reference & 3) != s->picture_structure) {
00334 my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
00335 if (refs[1][ref_n] < 0)
00336 nrefs[1] += 1;
00337 refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
00338 }
00339 }
00340 }
00341
00347 static void await_references(H264Context *h)
00348 {
00349 MpegEncContext *const s = &h->s;
00350 const int mb_xy = h->mb_xy;
00351 const int mb_type = s->current_picture.f.mb_type[mb_xy];
00352 int refs[2][48];
00353 int nrefs[2] = { 0 };
00354 int ref, list;
00355
00356 memset(refs, -1, sizeof(refs));
00357
00358 if (IS_16X16(mb_type)) {
00359 get_lowest_part_y(h, refs, 0, 16, 0,
00360 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
00361 } else if (IS_16X8(mb_type)) {
00362 get_lowest_part_y(h, refs, 0, 8, 0,
00363 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
00364 get_lowest_part_y(h, refs, 8, 8, 8,
00365 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
00366 } else if (IS_8X16(mb_type)) {
00367 get_lowest_part_y(h, refs, 0, 16, 0,
00368 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
00369 get_lowest_part_y(h, refs, 4, 16, 0,
00370 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
00371 } else {
00372 int i;
00373
00374 assert(IS_8X8(mb_type));
00375
00376 for (i = 0; i < 4; i++) {
00377 const int sub_mb_type = h->sub_mb_type[i];
00378 const int n = 4 * i;
00379 int y_offset = (i & 2) << 2;
00380
00381 if (IS_SUB_8X8(sub_mb_type)) {
00382 get_lowest_part_y(h, refs, n, 8, y_offset,
00383 IS_DIR(sub_mb_type, 0, 0),
00384 IS_DIR(sub_mb_type, 0, 1),
00385 nrefs);
00386 } else if (IS_SUB_8X4(sub_mb_type)) {
00387 get_lowest_part_y(h, refs, n, 4, y_offset,
00388 IS_DIR(sub_mb_type, 0, 0),
00389 IS_DIR(sub_mb_type, 0, 1),
00390 nrefs);
00391 get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
00392 IS_DIR(sub_mb_type, 0, 0),
00393 IS_DIR(sub_mb_type, 0, 1),
00394 nrefs);
00395 } else if (IS_SUB_4X8(sub_mb_type)) {
00396 get_lowest_part_y(h, refs, n, 8, y_offset,
00397 IS_DIR(sub_mb_type, 0, 0),
00398 IS_DIR(sub_mb_type, 0, 1),
00399 nrefs);
00400 get_lowest_part_y(h, refs, n + 1, 8, y_offset,
00401 IS_DIR(sub_mb_type, 0, 0),
00402 IS_DIR(sub_mb_type, 0, 1),
00403 nrefs);
00404 } else {
00405 int j;
00406 assert(IS_SUB_4X4(sub_mb_type));
00407 for (j = 0; j < 4; j++) {
00408 int sub_y_offset = y_offset + 2 * (j & 2);
00409 get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
00410 IS_DIR(sub_mb_type, 0, 0),
00411 IS_DIR(sub_mb_type, 0, 1),
00412 nrefs);
00413 }
00414 }
00415 }
00416 }
00417
00418 for (list = h->list_count - 1; list >= 0; list--)
00419 for (ref = 0; ref < 48 && nrefs[list]; ref++) {
00420 int row = refs[list][ref];
00421 if (row >= 0) {
00422 Picture *ref_pic = &h->ref_list[list][ref];
00423 int ref_field = ref_pic->f.reference - 1;
00424 int ref_field_picture = ref_pic->field_picture;
00425 int pic_height = 16 * s->mb_height >> ref_field_picture;
00426
00427 row <<= MB_MBAFF;
00428 nrefs[list]--;
00429
00430 if (!FIELD_PICTURE && ref_field_picture) {
00431 ff_thread_await_progress(&ref_pic->f,
00432 FFMIN((row >> 1) - !(row & 1),
00433 pic_height - 1),
00434 1);
00435 ff_thread_await_progress(&ref_pic->f,
00436 FFMIN((row >> 1), pic_height - 1),
00437 0);
00438 } else if (FIELD_PICTURE && !ref_field_picture) {
00439 ff_thread_await_progress(&ref_pic->f,
00440 FFMIN(row * 2 + ref_field,
00441 pic_height - 1),
00442 0);
00443 } else if (FIELD_PICTURE) {
00444 ff_thread_await_progress(&ref_pic->f,
00445 FFMIN(row, pic_height - 1),
00446 ref_field);
00447 } else {
00448 ff_thread_await_progress(&ref_pic->f,
00449 FFMIN(row, pic_height - 1),
00450 0);
00451 }
00452 }
00453 }
00454 }
00455
00456 static av_always_inline void mc_dir_part(H264Context *h, Picture *pic,
00457 int n, int square, int height,
00458 int delta, int list,
00459 uint8_t *dest_y, uint8_t *dest_cb,
00460 uint8_t *dest_cr,
00461 int src_x_offset, int src_y_offset,
00462 qpel_mc_func *qpix_op,
00463 h264_chroma_mc_func chroma_op,
00464 int pixel_shift, int chroma_idc)
00465 {
00466 MpegEncContext *const s = &h->s;
00467 const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
00468 int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
00469 const int luma_xy = (mx & 3) + ((my & 3) << 2);
00470 int offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
00471 uint8_t *src_y = pic->f.data[0] + offset;
00472 uint8_t *src_cb, *src_cr;
00473 int extra_width = h->emu_edge_width;
00474 int extra_height = h->emu_edge_height;
00475 int emu = 0;
00476 const int full_mx = mx >> 2;
00477 const int full_my = my >> 2;
00478 const int pic_width = 16 * s->mb_width;
00479 const int pic_height = 16 * s->mb_height >> MB_FIELD;
00480 int ysh;
00481
00482 if (mx & 7)
00483 extra_width -= 3;
00484 if (my & 7)
00485 extra_height -= 3;
00486
00487 if (full_mx < 0 - extra_width ||
00488 full_my < 0 - extra_height ||
00489 full_mx + 16 > pic_width + extra_width ||
00490 full_my + 16 > pic_height + extra_height) {
00491 s->dsp.emulated_edge_mc(s->edge_emu_buffer,
00492 src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
00493 h->mb_linesize,
00494 16 + 5, 16 + 5 , full_mx - 2,
00495 full_my - 2, pic_width, pic_height);
00496 src_y = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
00497 emu = 1;
00498 }
00499
00500 qpix_op[luma_xy](dest_y, src_y, h->mb_linesize);
00501 if (!square)
00502 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
00503
00504 if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
00505 return;
00506
00507 if (chroma_idc == 3 ) {
00508 src_cb = pic->f.data[1] + offset;
00509 if (emu) {
00510 s->dsp.emulated_edge_mc(s->edge_emu_buffer,
00511 src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
00512 h->mb_linesize,
00513 16 + 5, 16 + 5 ,
00514 full_mx - 2, full_my - 2,
00515 pic_width, pic_height);
00516 src_cb = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
00517 }
00518 qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize);
00519 if (!square)
00520 qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
00521
00522 src_cr = pic->f.data[2] + offset;
00523 if (emu) {
00524 s->dsp.emulated_edge_mc(s->edge_emu_buffer,
00525 src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
00526 h->mb_linesize,
00527 16 + 5, 16 + 5 ,
00528 full_mx - 2, full_my - 2,
00529 pic_width, pic_height);
00530 src_cr = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
00531 }
00532 qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize);
00533 if (!square)
00534 qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
00535 return;
00536 }
00537
00538 ysh = 3 - (chroma_idc == 2 );
00539 if (chroma_idc == 1 && MB_FIELD) {
00540
00541 my += 2 * ((s->mb_y & 1) - (pic->f.reference - 1));
00542 emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
00543 }
00544
00545 src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
00546 (my >> ysh) * h->mb_uvlinesize;
00547 src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
00548 (my >> ysh) * h->mb_uvlinesize;
00549
00550 if (emu) {
00551 s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize,
00552 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
00553 pic_width >> 1, pic_height >> (chroma_idc == 1 ));
00554 src_cb = s->edge_emu_buffer;
00555 }
00556 chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
00557 height >> (chroma_idc == 1 ),
00558 mx & 7, (my << (chroma_idc == 2 )) & 7);
00559
00560 if (emu) {
00561 s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize,
00562 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
00563 pic_width >> 1, pic_height >> (chroma_idc == 1 ));
00564 src_cr = s->edge_emu_buffer;
00565 }
00566 chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 ),
00567 mx & 7, (my << (chroma_idc == 2 )) & 7);
00568 }
00569
00570 static av_always_inline void mc_part_std(H264Context *h, int n, int square,
00571 int height, int delta,
00572 uint8_t *dest_y, uint8_t *dest_cb,
00573 uint8_t *dest_cr,
00574 int x_offset, int y_offset,
00575 qpel_mc_func *qpix_put,
00576 h264_chroma_mc_func chroma_put,
00577 qpel_mc_func *qpix_avg,
00578 h264_chroma_mc_func chroma_avg,
00579 int list0, int list1,
00580 int pixel_shift, int chroma_idc)
00581 {
00582 MpegEncContext *const s = &h->s;
00583 qpel_mc_func *qpix_op = qpix_put;
00584 h264_chroma_mc_func chroma_op = chroma_put;
00585
00586 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
00587 if (chroma_idc == 3 ) {
00588 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
00589 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
00590 } else if (chroma_idc == 2 ) {
00591 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
00592 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
00593 } else {
00594 dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
00595 dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
00596 }
00597 x_offset += 8 * s->mb_x;
00598 y_offset += 8 * (s->mb_y >> MB_FIELD);
00599
00600 if (list0) {
00601 Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
00602 mc_dir_part(h, ref, n, square, height, delta, 0,
00603 dest_y, dest_cb, dest_cr, x_offset, y_offset,
00604 qpix_op, chroma_op, pixel_shift, chroma_idc);
00605
00606 qpix_op = qpix_avg;
00607 chroma_op = chroma_avg;
00608 }
00609
00610 if (list1) {
00611 Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
00612 mc_dir_part(h, ref, n, square, height, delta, 1,
00613 dest_y, dest_cb, dest_cr, x_offset, y_offset,
00614 qpix_op, chroma_op, pixel_shift, chroma_idc);
00615 }
00616 }
00617
00618 static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
00619 int height, int delta,
00620 uint8_t *dest_y, uint8_t *dest_cb,
00621 uint8_t *dest_cr,
00622 int x_offset, int y_offset,
00623 qpel_mc_func *qpix_put,
00624 h264_chroma_mc_func chroma_put,
00625 h264_weight_func luma_weight_op,
00626 h264_weight_func chroma_weight_op,
00627 h264_biweight_func luma_weight_avg,
00628 h264_biweight_func chroma_weight_avg,
00629 int list0, int list1,
00630 int pixel_shift, int chroma_idc)
00631 {
00632 MpegEncContext *const s = &h->s;
00633 int chroma_height;
00634
00635 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
00636 if (chroma_idc == 3 ) {
00637 chroma_height = height;
00638 chroma_weight_avg = luma_weight_avg;
00639 chroma_weight_op = luma_weight_op;
00640 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
00641 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
00642 } else if (chroma_idc == 2 ) {
00643 chroma_height = height;
00644 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
00645 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
00646 } else {
00647 chroma_height = height >> 1;
00648 dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
00649 dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
00650 }
00651 x_offset += 8 * s->mb_x;
00652 y_offset += 8 * (s->mb_y >> MB_FIELD);
00653
00654 if (list0 && list1) {
00655
00656
00657 uint8_t *tmp_cb = s->obmc_scratchpad;
00658 uint8_t *tmp_cr = s->obmc_scratchpad + (16 << pixel_shift);
00659 uint8_t *tmp_y = s->obmc_scratchpad + 16 * h->mb_uvlinesize;
00660 int refn0 = h->ref_cache[0][scan8[n]];
00661 int refn1 = h->ref_cache[1][scan8[n]];
00662
00663 mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
00664 dest_y, dest_cb, dest_cr,
00665 x_offset, y_offset, qpix_put, chroma_put,
00666 pixel_shift, chroma_idc);
00667 mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
00668 tmp_y, tmp_cb, tmp_cr,
00669 x_offset, y_offset, qpix_put, chroma_put,
00670 pixel_shift, chroma_idc);
00671
00672 if (h->use_weight == 2) {
00673 int weight0 = h->implicit_weight[refn0][refn1][s->mb_y & 1];
00674 int weight1 = 64 - weight0;
00675 luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
00676 height, 5, weight0, weight1, 0);
00677 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
00678 chroma_height, 5, weight0, weight1, 0);
00679 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
00680 chroma_height, 5, weight0, weight1, 0);
00681 } else {
00682 luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
00683 h->luma_log2_weight_denom,
00684 h->luma_weight[refn0][0][0],
00685 h->luma_weight[refn1][1][0],
00686 h->luma_weight[refn0][0][1] +
00687 h->luma_weight[refn1][1][1]);
00688 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
00689 h->chroma_log2_weight_denom,
00690 h->chroma_weight[refn0][0][0][0],
00691 h->chroma_weight[refn1][1][0][0],
00692 h->chroma_weight[refn0][0][0][1] +
00693 h->chroma_weight[refn1][1][0][1]);
00694 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
00695 h->chroma_log2_weight_denom,
00696 h->chroma_weight[refn0][0][1][0],
00697 h->chroma_weight[refn1][1][1][0],
00698 h->chroma_weight[refn0][0][1][1] +
00699 h->chroma_weight[refn1][1][1][1]);
00700 }
00701 } else {
00702 int list = list1 ? 1 : 0;
00703 int refn = h->ref_cache[list][scan8[n]];
00704 Picture *ref = &h->ref_list[list][refn];
00705 mc_dir_part(h, ref, n, square, height, delta, list,
00706 dest_y, dest_cb, dest_cr, x_offset, y_offset,
00707 qpix_put, chroma_put, pixel_shift, chroma_idc);
00708
00709 luma_weight_op(dest_y, h->mb_linesize, height,
00710 h->luma_log2_weight_denom,
00711 h->luma_weight[refn][list][0],
00712 h->luma_weight[refn][list][1]);
00713 if (h->use_weight_chroma) {
00714 chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
00715 h->chroma_log2_weight_denom,
00716 h->chroma_weight[refn][list][0][0],
00717 h->chroma_weight[refn][list][0][1]);
00718 chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
00719 h->chroma_log2_weight_denom,
00720 h->chroma_weight[refn][list][1][0],
00721 h->chroma_weight[refn][list][1][1]);
00722 }
00723 }
00724 }
00725
00726 static av_always_inline void mc_part(H264Context *h, int n, int square,
00727 int height, int delta,
00728 uint8_t *dest_y, uint8_t *dest_cb,
00729 uint8_t *dest_cr,
00730 int x_offset, int y_offset,
00731 qpel_mc_func *qpix_put,
00732 h264_chroma_mc_func chroma_put,
00733 qpel_mc_func *qpix_avg,
00734 h264_chroma_mc_func chroma_avg,
00735 h264_weight_func *weight_op,
00736 h264_biweight_func *weight_avg,
00737 int list0, int list1,
00738 int pixel_shift, int chroma_idc)
00739 {
00740 if ((h->use_weight == 2 && list0 && list1 &&
00741 (h->implicit_weight[h->ref_cache[0][scan8[n]]][h->ref_cache[1][scan8[n]]][h->s.mb_y & 1] != 32)) ||
00742 h->use_weight == 1)
00743 mc_part_weighted(h, n, square, height, delta, dest_y, dest_cb, dest_cr,
00744 x_offset, y_offset, qpix_put, chroma_put,
00745 weight_op[0], weight_op[1], weight_avg[0],
00746 weight_avg[1], list0, list1, pixel_shift, chroma_idc);
00747 else
00748 mc_part_std(h, n, square, height, delta, dest_y, dest_cb, dest_cr,
00749 x_offset, y_offset, qpix_put, chroma_put, qpix_avg,
00750 chroma_avg, list0, list1, pixel_shift, chroma_idc);
00751 }
00752
00753 static av_always_inline void prefetch_motion(H264Context *h, int list,
00754 int pixel_shift, int chroma_idc)
00755 {
00756
00757
00758 MpegEncContext *const s = &h->s;
00759 const int refn = h->ref_cache[list][scan8[0]];
00760 if (refn >= 0) {
00761 const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * s->mb_x + 8;
00762 const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * s->mb_y;
00763 uint8_t **src = h->ref_list[list][refn].f.data;
00764 int off = (mx << pixel_shift) +
00765 (my + (s->mb_x & 3) * 4) * h->mb_linesize +
00766 (64 << pixel_shift);
00767 s->dsp.prefetch(src[0] + off, s->linesize, 4);
00768 if (chroma_idc == 3 ) {
00769 s->dsp.prefetch(src[1] + off, s->linesize, 4);
00770 s->dsp.prefetch(src[2] + off, s->linesize, 4);
00771 } else {
00772 off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (s->mb_x&7))*s->uvlinesize;
00773 s->dsp.prefetch(src[1] + off, src[2] - src[1], 2);
00774 }
00775 }
00776 }
00777
00778 static av_always_inline void hl_motion(H264Context *h, uint8_t *dest_y,
00779 uint8_t *dest_cb, uint8_t *dest_cr,
00780 qpel_mc_func(*qpix_put)[16],
00781 h264_chroma_mc_func(*chroma_put),
00782 qpel_mc_func(*qpix_avg)[16],
00783 h264_chroma_mc_func(*chroma_avg),
00784 h264_weight_func *weight_op,
00785 h264_biweight_func *weight_avg,
00786 int pixel_shift, int chroma_idc)
00787 {
00788 MpegEncContext *const s = &h->s;
00789 const int mb_xy = h->mb_xy;
00790 const int mb_type = s->current_picture.f.mb_type[mb_xy];
00791
00792 assert(IS_INTER(mb_type));
00793
00794 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
00795 await_references(h);
00796 prefetch_motion(h, 0, pixel_shift, chroma_idc);
00797
00798 if (IS_16X16(mb_type)) {
00799 mc_part(h, 0, 1, 16, 0, dest_y, dest_cb, dest_cr, 0, 0,
00800 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
00801 weight_op, weight_avg,
00802 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
00803 pixel_shift, chroma_idc);
00804 } else if (IS_16X8(mb_type)) {
00805 mc_part(h, 0, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,
00806 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
00807 weight_op, weight_avg,
00808 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
00809 pixel_shift, chroma_idc);
00810 mc_part(h, 8, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,
00811 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
00812 weight_op, weight_avg,
00813 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
00814 pixel_shift, chroma_idc);
00815 } else if (IS_8X16(mb_type)) {
00816 mc_part(h, 0, 0, 16, 8 * h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
00817 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00818 &weight_op[1], &weight_avg[1],
00819 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
00820 pixel_shift, chroma_idc);
00821 mc_part(h, 4, 0, 16, 8 * h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
00822 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00823 &weight_op[1], &weight_avg[1],
00824 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
00825 pixel_shift, chroma_idc);
00826 } else {
00827 int i;
00828
00829 assert(IS_8X8(mb_type));
00830
00831 for (i = 0; i < 4; i++) {
00832 const int sub_mb_type = h->sub_mb_type[i];
00833 const int n = 4 * i;
00834 int x_offset = (i & 1) << 2;
00835 int y_offset = (i & 2) << 1;
00836
00837 if (IS_SUB_8X8(sub_mb_type)) {
00838 mc_part(h, n, 1, 8, 0, dest_y, dest_cb, dest_cr,
00839 x_offset, y_offset,
00840 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00841 &weight_op[1], &weight_avg[1],
00842 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00843 pixel_shift, chroma_idc);
00844 } else if (IS_SUB_8X4(sub_mb_type)) {
00845 mc_part(h, n, 0, 4, 4 << pixel_shift, dest_y, dest_cb, dest_cr,
00846 x_offset, y_offset,
00847 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
00848 &weight_op[1], &weight_avg[1],
00849 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00850 pixel_shift, chroma_idc);
00851 mc_part(h, n + 2, 0, 4, 4 << pixel_shift,
00852 dest_y, dest_cb, dest_cr, x_offset, y_offset + 2,
00853 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
00854 &weight_op[1], &weight_avg[1],
00855 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00856 pixel_shift, chroma_idc);
00857 } else if (IS_SUB_4X8(sub_mb_type)) {
00858 mc_part(h, n, 0, 8, 4 * h->mb_linesize,
00859 dest_y, dest_cb, dest_cr, x_offset, y_offset,
00860 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00861 &weight_op[2], &weight_avg[2],
00862 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00863 pixel_shift, chroma_idc);
00864 mc_part(h, n + 1, 0, 8, 4 * h->mb_linesize,
00865 dest_y, dest_cb, dest_cr, x_offset + 2, y_offset,
00866 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00867 &weight_op[2], &weight_avg[2],
00868 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00869 pixel_shift, chroma_idc);
00870 } else {
00871 int j;
00872 assert(IS_SUB_4X4(sub_mb_type));
00873 for (j = 0; j < 4; j++) {
00874 int sub_x_offset = x_offset + 2 * (j & 1);
00875 int sub_y_offset = y_offset + (j & 2);
00876 mc_part(h, n + j, 1, 4, 0,
00877 dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
00878 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00879 &weight_op[2], &weight_avg[2],
00880 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00881 pixel_shift, chroma_idc);
00882 }
00883 }
00884 }
00885 }
00886
00887 prefetch_motion(h, 1, pixel_shift, chroma_idc);
00888 }
00889
00890 static av_always_inline void hl_motion_420(H264Context *h, uint8_t *dest_y,
00891 uint8_t *dest_cb, uint8_t *dest_cr,
00892 qpel_mc_func(*qpix_put)[16],
00893 h264_chroma_mc_func(*chroma_put),
00894 qpel_mc_func(*qpix_avg)[16],
00895 h264_chroma_mc_func(*chroma_avg),
00896 h264_weight_func *weight_op,
00897 h264_biweight_func *weight_avg,
00898 int pixel_shift)
00899 {
00900 hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put,
00901 qpix_avg, chroma_avg, weight_op, weight_avg, pixel_shift, 1);
00902 }
00903
00904 static av_always_inline void hl_motion_422(H264Context *h, uint8_t *dest_y,
00905 uint8_t *dest_cb, uint8_t *dest_cr,
00906 qpel_mc_func(*qpix_put)[16],
00907 h264_chroma_mc_func(*chroma_put),
00908 qpel_mc_func(*qpix_avg)[16],
00909 h264_chroma_mc_func(*chroma_avg),
00910 h264_weight_func *weight_op,
00911 h264_biweight_func *weight_avg,
00912 int pixel_shift)
00913 {
00914 hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put,
00915 qpix_avg, chroma_avg, weight_op, weight_avg, pixel_shift, 2);
00916 }
00917
00918 static void free_tables(H264Context *h, int free_rbsp)
00919 {
00920 int i;
00921 H264Context *hx;
00922
00923 av_freep(&h->intra4x4_pred_mode);
00924 av_freep(&h->chroma_pred_mode_table);
00925 av_freep(&h->cbp_table);
00926 av_freep(&h->mvd_table[0]);
00927 av_freep(&h->mvd_table[1]);
00928 av_freep(&h->direct_table);
00929 av_freep(&h->non_zero_count);
00930 av_freep(&h->slice_table_base);
00931 h->slice_table = NULL;
00932 av_freep(&h->list_counts);
00933
00934 av_freep(&h->mb2b_xy);
00935 av_freep(&h->mb2br_xy);
00936
00937 for (i = 0; i < MAX_THREADS; i++) {
00938 hx = h->thread_context[i];
00939 if (!hx)
00940 continue;
00941 av_freep(&hx->top_borders[1]);
00942 av_freep(&hx->top_borders[0]);
00943 av_freep(&hx->s.obmc_scratchpad);
00944 if (free_rbsp) {
00945 av_freep(&hx->rbsp_buffer[1]);
00946 av_freep(&hx->rbsp_buffer[0]);
00947 hx->rbsp_buffer_size[0] = 0;
00948 hx->rbsp_buffer_size[1] = 0;
00949 }
00950 if (i)
00951 av_freep(&h->thread_context[i]);
00952 }
00953 }
00954
00955 static void init_dequant8_coeff_table(H264Context *h)
00956 {
00957 int i, j, q, x;
00958 const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
00959
00960 for (i = 0; i < 6; i++) {
00961 h->dequant8_coeff[i] = h->dequant8_buffer[i];
00962 for (j = 0; j < i; j++)
00963 if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
00964 64 * sizeof(uint8_t))) {
00965 h->dequant8_coeff[i] = h->dequant8_buffer[j];
00966 break;
00967 }
00968 if (j < i)
00969 continue;
00970
00971 for (q = 0; q < max_qp + 1; q++) {
00972 int shift = div6[q];
00973 int idx = rem6[q];
00974 for (x = 0; x < 64; x++)
00975 h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
00976 ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
00977 h->pps.scaling_matrix8[i][x]) << shift;
00978 }
00979 }
00980 }
00981
00982 static void init_dequant4_coeff_table(H264Context *h)
00983 {
00984 int i, j, q, x;
00985 const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
00986 for (i = 0; i < 6; i++) {
00987 h->dequant4_coeff[i] = h->dequant4_buffer[i];
00988 for (j = 0; j < i; j++)
00989 if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
00990 16 * sizeof(uint8_t))) {
00991 h->dequant4_coeff[i] = h->dequant4_buffer[j];
00992 break;
00993 }
00994 if (j < i)
00995 continue;
00996
00997 for (q = 0; q < max_qp + 1; q++) {
00998 int shift = div6[q] + 2;
00999 int idx = rem6[q];
01000 for (x = 0; x < 16; x++)
01001 h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
01002 ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
01003 h->pps.scaling_matrix4[i][x]) << shift;
01004 }
01005 }
01006 }
01007
01008 static void init_dequant_tables(H264Context *h)
01009 {
01010 int i, x;
01011 init_dequant4_coeff_table(h);
01012 if (h->pps.transform_8x8_mode)
01013 init_dequant8_coeff_table(h);
01014 if (h->sps.transform_bypass) {
01015 for (i = 0; i < 6; i++)
01016 for (x = 0; x < 16; x++)
01017 h->dequant4_coeff[i][0][x] = 1 << 6;
01018 if (h->pps.transform_8x8_mode)
01019 for (i = 0; i < 6; i++)
01020 for (x = 0; x < 64; x++)
01021 h->dequant8_coeff[i][0][x] = 1 << 6;
01022 }
01023 }
01024
01025 int ff_h264_alloc_tables(H264Context *h)
01026 {
01027 MpegEncContext *const s = &h->s;
01028 const int big_mb_num = s->mb_stride * (s->mb_height + 1);
01029 const int row_mb_num = 2*s->mb_stride*FFMAX(s->avctx->thread_count, 1);
01030 int x, y;
01031
01032 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode,
01033 row_mb_num * 8 * sizeof(uint8_t), fail)
01034 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count,
01035 big_mb_num * 48 * sizeof(uint8_t), fail)
01036 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base,
01037 (big_mb_num + s->mb_stride) * sizeof(*h->slice_table_base), fail)
01038 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table,
01039 big_mb_num * sizeof(uint16_t), fail)
01040 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table,
01041 big_mb_num * sizeof(uint8_t), fail)
01042 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0],
01043 16 * row_mb_num * sizeof(uint8_t), fail);
01044 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1],
01045 16 * row_mb_num * sizeof(uint8_t), fail);
01046 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table,
01047 4 * big_mb_num * sizeof(uint8_t), fail);
01048 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts,
01049 big_mb_num * sizeof(uint8_t), fail)
01050
01051 memset(h->slice_table_base, -1,
01052 (big_mb_num + s->mb_stride) * sizeof(*h->slice_table_base));
01053 h->slice_table = h->slice_table_base + s->mb_stride * 2 + 1;
01054
01055 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy,
01056 big_mb_num * sizeof(uint32_t), fail);
01057 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy,
01058 big_mb_num * sizeof(uint32_t), fail);
01059 for (y = 0; y < s->mb_height; y++)
01060 for (x = 0; x < s->mb_width; x++) {
01061 const int mb_xy = x + y * s->mb_stride;
01062 const int b_xy = 4 * x + 4 * y * h->b_stride;
01063
01064 h->mb2b_xy[mb_xy] = b_xy;
01065 h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * s->mb_stride)));
01066 }
01067
01068 s->obmc_scratchpad = NULL;
01069
01070 if (!h->dequant4_coeff[0])
01071 init_dequant_tables(h);
01072
01073 return 0;
01074
01075 fail:
01076 free_tables(h, 1);
01077 return -1;
01078 }
01079
01083 static void clone_tables(H264Context *dst, H264Context *src, int i)
01084 {
01085 MpegEncContext *const s = &src->s;
01086 dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * s->mb_stride;
01087 dst->non_zero_count = src->non_zero_count;
01088 dst->slice_table = src->slice_table;
01089 dst->cbp_table = src->cbp_table;
01090 dst->mb2b_xy = src->mb2b_xy;
01091 dst->mb2br_xy = src->mb2br_xy;
01092 dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
01093 dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * s->mb_stride;
01094 dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * s->mb_stride;
01095 dst->direct_table = src->direct_table;
01096 dst->list_counts = src->list_counts;
01097 dst->s.obmc_scratchpad = NULL;
01098 ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma,
01099 src->sps.chroma_format_idc);
01100 }
01101
01106 static int context_init(H264Context *h)
01107 {
01108 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0],
01109 h->s.mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
01110 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1],
01111 h->s.mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
01112
01113 h->ref_cache[0][scan8[5] + 1] =
01114 h->ref_cache[0][scan8[7] + 1] =
01115 h->ref_cache[0][scan8[13] + 1] =
01116 h->ref_cache[1][scan8[5] + 1] =
01117 h->ref_cache[1][scan8[7] + 1] =
01118 h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
01119
01120 return 0;
01121
01122 fail:
01123 return -1;
01124 }
01125
01126 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
01127
01128 static av_cold void common_init(H264Context *h)
01129 {
01130 MpegEncContext *const s = &h->s;
01131
01132 s->width = s->avctx->width;
01133 s->height = s->avctx->height;
01134 s->codec_id = s->avctx->codec->id;
01135
01136 s->avctx->bits_per_raw_sample = 8;
01137 h->cur_chroma_format_idc = 1;
01138
01139 ff_h264dsp_init(&h->h264dsp,
01140 s->avctx->bits_per_raw_sample, h->cur_chroma_format_idc);
01141 ff_h264_pred_init(&h->hpc, s->codec_id,
01142 s->avctx->bits_per_raw_sample, h->cur_chroma_format_idc);
01143
01144 h->dequant_coeff_pps = -1;
01145 s->unrestricted_mv = 1;
01146
01147 s->dsp.dct_bits = 16;
01148
01149 ff_dsputil_init(&s->dsp, s->avctx);
01150
01151 memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
01152 memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
01153 }
01154
01155 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
01156 {
01157 AVCodecContext *avctx = h->s.avctx;
01158
01159 if (!buf || size <= 0)
01160 return -1;
01161
01162 if (buf[0] == 1) {
01163 int i, cnt, nalsize;
01164 const unsigned char *p = buf;
01165
01166 h->is_avc = 1;
01167
01168 if (size < 7) {
01169 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
01170 return -1;
01171 }
01172
01173
01174 h->nal_length_size = 2;
01175
01176 cnt = *(p + 5) & 0x1f;
01177 p += 6;
01178 for (i = 0; i < cnt; i++) {
01179 nalsize = AV_RB16(p) + 2;
01180 if(nalsize > size - (p-buf))
01181 return -1;
01182 if (decode_nal_units(h, p, nalsize) < 0) {
01183 av_log(avctx, AV_LOG_ERROR,
01184 "Decoding sps %d from avcC failed\n", i);
01185 return -1;
01186 }
01187 p += nalsize;
01188 }
01189
01190 cnt = *(p++);
01191 for (i = 0; i < cnt; i++) {
01192 nalsize = AV_RB16(p) + 2;
01193 if(nalsize > size - (p-buf))
01194 return -1;
01195 if (decode_nal_units(h, p, nalsize) < 0) {
01196 av_log(avctx, AV_LOG_ERROR,
01197 "Decoding pps %d from avcC failed\n", i);
01198 return -1;
01199 }
01200 p += nalsize;
01201 }
01202
01203 h->nal_length_size = (buf[4] & 0x03) + 1;
01204 } else {
01205 h->is_avc = 0;
01206 if (decode_nal_units(h, buf, size) < 0)
01207 return -1;
01208 }
01209 return size;
01210 }
01211
01212 av_cold int ff_h264_decode_init(AVCodecContext *avctx)
01213 {
01214 H264Context *h = avctx->priv_data;
01215 MpegEncContext *const s = &h->s;
01216 int i;
01217
01218 ff_MPV_decode_defaults(s);
01219
01220 s->avctx = avctx;
01221 common_init(h);
01222
01223 s->out_format = FMT_H264;
01224 s->workaround_bugs = avctx->workaround_bugs;
01225
01226
01227
01228 s->quarter_sample = 1;
01229 if (!avctx->has_b_frames)
01230 s->low_delay = 1;
01231
01232 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
01233
01234 ff_h264_decode_init_vlc();
01235
01236 h->pixel_shift = 0;
01237 h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
01238
01239 h->thread_context[0] = h;
01240 h->outputed_poc = h->next_outputed_poc = INT_MIN;
01241 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
01242 h->last_pocs[i] = INT_MIN;
01243 h->prev_poc_msb = 1 << 16;
01244 h->prev_frame_num = -1;
01245 h->x264_build = -1;
01246 ff_h264_reset_sei(h);
01247 if (avctx->codec_id == CODEC_ID_H264) {
01248 if (avctx->ticks_per_frame == 1)
01249 s->avctx->time_base.den *= 2;
01250 avctx->ticks_per_frame = 2;
01251 }
01252
01253 if (avctx->extradata_size > 0 && avctx->extradata &&
01254 ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size) < 0) {
01255 ff_h264_free_context(h);
01256 return -1;
01257 }
01258
01259 if (h->sps.bitstream_restriction_flag &&
01260 s->avctx->has_b_frames < h->sps.num_reorder_frames) {
01261 s->avctx->has_b_frames = h->sps.num_reorder_frames;
01262 s->low_delay = 0;
01263 }
01264
01265 return 0;
01266 }
01267
01268 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
01269
01270 static void copy_picture_range(Picture **to, Picture **from, int count,
01271 MpegEncContext *new_base,
01272 MpegEncContext *old_base)
01273 {
01274 int i;
01275
01276 for (i = 0; i < count; i++) {
01277 assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
01278 IN_RANGE(from[i], old_base->picture,
01279 sizeof(Picture) * old_base->picture_count) ||
01280 !from[i]));
01281 to[i] = REBASE_PICTURE(from[i], new_base, old_base);
01282 }
01283 }
01284
01285 static void copy_parameter_set(void **to, void **from, int count, int size)
01286 {
01287 int i;
01288
01289 for (i = 0; i < count; i++) {
01290 if (to[i] && !from[i])
01291 av_freep(&to[i]);
01292 else if (from[i] && !to[i])
01293 to[i] = av_malloc(size);
01294
01295 if (from[i])
01296 memcpy(to[i], from[i], size);
01297 }
01298 }
01299
01300 static int decode_init_thread_copy(AVCodecContext *avctx)
01301 {
01302 H264Context *h = avctx->priv_data;
01303
01304 if (!avctx->internal->is_copy)
01305 return 0;
01306 memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
01307 memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
01308
01309 return 0;
01310 }
01311
01312 #define copy_fields(to, from, start_field, end_field) \
01313 memcpy(&to->start_field, &from->start_field, \
01314 (char *)&to->end_field - (char *)&to->start_field)
01315
01316 static int decode_update_thread_context(AVCodecContext *dst,
01317 const AVCodecContext *src)
01318 {
01319 H264Context *h = dst->priv_data, *h1 = src->priv_data;
01320 MpegEncContext *const s = &h->s, *const s1 = &h1->s;
01321 int inited = s->context_initialized, err;
01322 int i;
01323
01324 if (dst == src)
01325 return 0;
01326
01327 err = ff_mpeg_update_thread_context(dst, src);
01328 if (err)
01329 return err;
01330
01331
01332 if (!inited) {
01333 for (i = 0; i < MAX_SPS_COUNT; i++)
01334 av_freep(h->sps_buffers + i);
01335
01336 for (i = 0; i < MAX_PPS_COUNT; i++)
01337 av_freep(h->pps_buffers + i);
01338
01339
01340 memcpy(&h->s + 1, &h1->s + 1,
01341 sizeof(H264Context) - sizeof(MpegEncContext));
01342 memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
01343 memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
01344
01345 if (s1->context_initialized) {
01346 if (ff_h264_alloc_tables(h) < 0) {
01347 av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
01348 return AVERROR(ENOMEM);
01349 }
01350 context_init(h);
01351
01352
01353
01354 h->s.obmc_scratchpad = av_malloc(16 * 6 * s->linesize);
01355 }
01356
01357 for (i = 0; i < 2; i++) {
01358 h->rbsp_buffer[i] = NULL;
01359 h->rbsp_buffer_size[i] = 0;
01360 }
01361
01362 h->thread_context[0] = h;
01363
01364 s->dsp.clear_blocks(h->mb);
01365 s->dsp.clear_blocks(h->mb + (24 * 16 << h->pixel_shift));
01366 }
01367
01368
01369 h->is_avc = h1->is_avc;
01370
01371
01372 copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
01373 MAX_SPS_COUNT, sizeof(SPS));
01374 h->sps = h1->sps;
01375 copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
01376 MAX_PPS_COUNT, sizeof(PPS));
01377 h->pps = h1->pps;
01378
01379
01380
01381 copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
01382
01383 for (i = 0; i < 6; i++)
01384 h->dequant4_coeff[i] = h->dequant4_buffer[0] +
01385 (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
01386
01387 for (i = 0; i < 6; i++)
01388 h->dequant8_coeff[i] = h->dequant8_buffer[0] +
01389 (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
01390
01391 h->dequant_coeff_pps = h1->dequant_coeff_pps;
01392
01393
01394 copy_fields(h, h1, poc_lsb, redundant_pic_count);
01395
01396
01397 copy_fields(h, h1, ref_count, list_count);
01398 copy_fields(h, h1, ref_list, intra_gb);
01399 copy_fields(h, h1, short_ref, cabac_init_idc);
01400
01401 copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1);
01402 copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1);
01403 copy_picture_range(h->delayed_pic, h1->delayed_pic,
01404 MAX_DELAYED_PIC_COUNT + 2, s, s1);
01405
01406 h->last_slice_type = h1->last_slice_type;
01407 h->sync = h1->sync;
01408
01409 if (!s->current_picture_ptr)
01410 return 0;
01411
01412 if (!s->dropable) {
01413 err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
01414 h->prev_poc_msb = h->poc_msb;
01415 h->prev_poc_lsb = h->poc_lsb;
01416 }
01417 h->prev_frame_num_offset = h->frame_num_offset;
01418 h->prev_frame_num = h->frame_num;
01419 h->outputed_poc = h->next_outputed_poc;
01420
01421 return err;
01422 }
01423
01424 int ff_h264_frame_start(H264Context *h)
01425 {
01426 MpegEncContext *const s = &h->s;
01427 int i;
01428 const int pixel_shift = h->pixel_shift;
01429
01430 if (ff_MPV_frame_start(s, s->avctx) < 0)
01431 return -1;
01432 ff_er_frame_start(s);
01433
01434
01435
01436
01437
01438
01439 s->current_picture_ptr->f.key_frame = 0;
01440 s->current_picture_ptr->sync = 0;
01441 s->current_picture_ptr->mmco_reset = 0;
01442
01443 assert(s->linesize && s->uvlinesize);
01444
01445 for (i = 0; i < 16; i++) {
01446 h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * s->linesize * ((scan8[i] - scan8[0]) >> 3);
01447 h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * s->linesize * ((scan8[i] - scan8[0]) >> 3);
01448 }
01449 for (i = 0; i < 16; i++) {
01450 h->block_offset[16 + i] =
01451 h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * s->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
01452 h->block_offset[48 + 16 + i] =
01453 h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * s->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
01454 }
01455
01456
01457
01458 for (i = 0; i < s->slice_context_count; i++)
01459 if (h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
01460 h->thread_context[i]->s.obmc_scratchpad = av_malloc(16 * 6 * s->linesize);
01461
01462
01463
01464 memset(h->slice_table, -1,
01465 (s->mb_height * s->mb_stride - 1) * sizeof(*h->slice_table));
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476 if (s->codec_id != CODEC_ID_SVQ3)
01477 s->current_picture_ptr->f.reference = 0;
01478
01479 s->current_picture_ptr->field_poc[0] =
01480 s->current_picture_ptr->field_poc[1] = INT_MAX;
01481
01482 h->next_output_pic = NULL;
01483
01484 assert(s->current_picture_ptr->long_ref == 0);
01485
01486 return 0;
01487 }
01488
01497 static void decode_postinit(H264Context *h, int setup_finished)
01498 {
01499 MpegEncContext *const s = &h->s;
01500 Picture *out = s->current_picture_ptr;
01501 Picture *cur = s->current_picture_ptr;
01502 int i, pics, out_of_order, out_idx;
01503
01504 s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_H264;
01505 s->current_picture_ptr->f.pict_type = s->pict_type;
01506
01507 if (h->next_output_pic)
01508 return;
01509
01510 if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
01511
01512
01513
01514
01515
01516
01517 return;
01518 }
01519
01520 cur->f.interlaced_frame = 0;
01521 cur->f.repeat_pict = 0;
01522
01523
01524
01525
01526
01527 if (h->sps.pic_struct_present_flag) {
01528 switch (h->sei_pic_struct) {
01529 case SEI_PIC_STRUCT_FRAME:
01530 break;
01531 case SEI_PIC_STRUCT_TOP_FIELD:
01532 case SEI_PIC_STRUCT_BOTTOM_FIELD:
01533 cur->f.interlaced_frame = 1;
01534 break;
01535 case SEI_PIC_STRUCT_TOP_BOTTOM:
01536 case SEI_PIC_STRUCT_BOTTOM_TOP:
01537 if (FIELD_OR_MBAFF_PICTURE)
01538 cur->f.interlaced_frame = 1;
01539 else
01540
01541 cur->f.interlaced_frame = h->prev_interlaced_frame;
01542 break;
01543 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
01544 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
01545
01546
01547
01548 cur->f.repeat_pict = 1;
01549 break;
01550 case SEI_PIC_STRUCT_FRAME_DOUBLING:
01551
01552 cur->f.repeat_pict = 2;
01553 break;
01554 case SEI_PIC_STRUCT_FRAME_TRIPLING:
01555 cur->f.repeat_pict = 4;
01556 break;
01557 }
01558
01559 if ((h->sei_ct_type & 3) &&
01560 h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
01561 cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
01562 } else {
01563
01564 cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE;
01565 }
01566 h->prev_interlaced_frame = cur->f.interlaced_frame;
01567
01568 if (cur->field_poc[0] != cur->field_poc[1]) {
01569
01570 cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
01571 } else {
01572 if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
01573
01574
01575 if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
01576 h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
01577 cur->f.top_field_first = 1;
01578 else
01579 cur->f.top_field_first = 0;
01580 } else {
01581
01582 cur->f.top_field_first = 0;
01583 }
01584 }
01585
01586 cur->mmco_reset = h->mmco_reset;
01587 h->mmco_reset = 0;
01588
01589
01590
01591
01592 if (h->sps.bitstream_restriction_flag &&
01593 s->avctx->has_b_frames < h->sps.num_reorder_frames) {
01594 s->avctx->has_b_frames = h->sps.num_reorder_frames;
01595 s->low_delay = 0;
01596 }
01597
01598 if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
01599 !h->sps.bitstream_restriction_flag) {
01600 s->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
01601 s->low_delay = 0;
01602 }
01603
01604 for (i = 0; 1; i++) {
01605 if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
01606 if(i)
01607 h->last_pocs[i-1] = cur->poc;
01608 break;
01609 } else if(i) {
01610 h->last_pocs[i-1]= h->last_pocs[i];
01611 }
01612 }
01613 out_of_order = MAX_DELAYED_PIC_COUNT - i;
01614 if( cur->f.pict_type == AV_PICTURE_TYPE_B
01615 || (h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > INT_MIN && h->last_pocs[MAX_DELAYED_PIC_COUNT-1] - h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > 2))
01616 out_of_order = FFMAX(out_of_order, 1);
01617 if(s->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
01618 av_log(s->avctx, AV_LOG_VERBOSE, "Increasing reorder buffer to %d\n", out_of_order);
01619 s->avctx->has_b_frames = out_of_order;
01620 s->low_delay = 0;
01621 }
01622
01623 pics = 0;
01624 while (h->delayed_pic[pics])
01625 pics++;
01626
01627 av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
01628
01629 h->delayed_pic[pics++] = cur;
01630 if (cur->f.reference == 0)
01631 cur->f.reference = DELAYED_PIC_REF;
01632
01633 out = h->delayed_pic[0];
01634 out_idx = 0;
01635 for (i = 1; h->delayed_pic[i] &&
01636 !h->delayed_pic[i]->f.key_frame &&
01637 !h->delayed_pic[i]->mmco_reset;
01638 i++)
01639 if (h->delayed_pic[i]->poc < out->poc) {
01640 out = h->delayed_pic[i];
01641 out_idx = i;
01642 }
01643 if (s->avctx->has_b_frames == 0 &&
01644 (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
01645 h->next_outputed_poc = INT_MIN;
01646 out_of_order = out->poc < h->next_outputed_poc;
01647
01648 if (out_of_order || pics > s->avctx->has_b_frames) {
01649 out->f.reference &= ~DELAYED_PIC_REF;
01650
01651
01652 out->owner2 = s;
01653 for (i = out_idx; h->delayed_pic[i]; i++)
01654 h->delayed_pic[i] = h->delayed_pic[i + 1];
01655 }
01656 if (!out_of_order && pics > s->avctx->has_b_frames) {
01657 h->next_output_pic = out;
01658 if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
01659 h->next_outputed_poc = INT_MIN;
01660 } else
01661 h->next_outputed_poc = out->poc;
01662 } else {
01663 av_log(s->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
01664 }
01665
01666 if (h->next_output_pic && h->next_output_pic->sync) {
01667 h->sync |= 2;
01668 }
01669
01670 if (setup_finished)
01671 ff_thread_finish_setup(s->avctx);
01672 }
01673
01674 static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
01675 uint8_t *src_cb, uint8_t *src_cr,
01676 int linesize, int uvlinesize,
01677 int simple)
01678 {
01679 MpegEncContext *const s = &h->s;
01680 uint8_t *top_border;
01681 int top_idx = 1;
01682 const int pixel_shift = h->pixel_shift;
01683 int chroma444 = CHROMA444;
01684 int chroma422 = CHROMA422;
01685
01686 src_y -= linesize;
01687 src_cb -= uvlinesize;
01688 src_cr -= uvlinesize;
01689
01690 if (!simple && FRAME_MBAFF) {
01691 if (s->mb_y & 1) {
01692 if (!MB_MBAFF) {
01693 top_border = h->top_borders[0][s->mb_x];
01694 AV_COPY128(top_border, src_y + 15 * linesize);
01695 if (pixel_shift)
01696 AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
01697 if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
01698 if (chroma444) {
01699 if (pixel_shift) {
01700 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
01701 AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
01702 AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
01703 AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
01704 } else {
01705 AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
01706 AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
01707 }
01708 } else if (chroma422) {
01709 if (pixel_shift) {
01710 AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
01711 AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
01712 } else {
01713 AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
01714 AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
01715 }
01716 } else {
01717 if (pixel_shift) {
01718 AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
01719 AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
01720 } else {
01721 AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
01722 AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
01723 }
01724 }
01725 }
01726 }
01727 } else if (MB_MBAFF) {
01728 top_idx = 0;
01729 } else
01730 return;
01731 }
01732
01733 top_border = h->top_borders[top_idx][s->mb_x];
01734
01735
01736 AV_COPY128(top_border, src_y + 16 * linesize);
01737 if (pixel_shift)
01738 AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
01739
01740 if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
01741 if (chroma444) {
01742 if (pixel_shift) {
01743 AV_COPY128(top_border + 32, src_cb + 16 * linesize);
01744 AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
01745 AV_COPY128(top_border + 64, src_cr + 16 * linesize);
01746 AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
01747 } else {
01748 AV_COPY128(top_border + 16, src_cb + 16 * linesize);
01749 AV_COPY128(top_border + 32, src_cr + 16 * linesize);
01750 }
01751 } else if (chroma422) {
01752 if (pixel_shift) {
01753 AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
01754 AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
01755 } else {
01756 AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
01757 AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
01758 }
01759 } else {
01760 if (pixel_shift) {
01761 AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
01762 AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
01763 } else {
01764 AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
01765 AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
01766 }
01767 }
01768 }
01769 }
01770
01771 static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
01772 uint8_t *src_cb, uint8_t *src_cr,
01773 int linesize, int uvlinesize,
01774 int xchg, int chroma444,
01775 int simple, int pixel_shift)
01776 {
01777 MpegEncContext *const s = &h->s;
01778 int deblock_topleft;
01779 int deblock_top;
01780 int top_idx = 1;
01781 uint8_t *top_border_m1;
01782 uint8_t *top_border;
01783
01784 if (!simple && FRAME_MBAFF) {
01785 if (s->mb_y & 1) {
01786 if (!MB_MBAFF)
01787 return;
01788 } else {
01789 top_idx = MB_MBAFF ? 0 : 1;
01790 }
01791 }
01792
01793 if (h->deblocking_filter == 2) {
01794 deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
01795 deblock_top = h->top_type;
01796 } else {
01797 deblock_topleft = (s->mb_x > 0);
01798 deblock_top = (s->mb_y > !!MB_FIELD);
01799 }
01800
01801 src_y -= linesize + 1 + pixel_shift;
01802 src_cb -= uvlinesize + 1 + pixel_shift;
01803 src_cr -= uvlinesize + 1 + pixel_shift;
01804
01805 top_border_m1 = h->top_borders[top_idx][s->mb_x - 1];
01806 top_border = h->top_borders[top_idx][s->mb_x];
01807
01808 #define XCHG(a, b, xchg) \
01809 if (pixel_shift) { \
01810 if (xchg) { \
01811 AV_SWAP64(b + 0, a + 0); \
01812 AV_SWAP64(b + 8, a + 8); \
01813 } else { \
01814 AV_COPY128(b, a); \
01815 } \
01816 } else if (xchg) \
01817 AV_SWAP64(b, a); \
01818 else \
01819 AV_COPY64(b, a);
01820
01821 if (deblock_top) {
01822 if (deblock_topleft) {
01823 XCHG(top_border_m1 + (8 << pixel_shift),
01824 src_y - (7 << pixel_shift), 1);
01825 }
01826 XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
01827 XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
01828 if (s->mb_x + 1 < s->mb_width) {
01829 XCHG(h->top_borders[top_idx][s->mb_x + 1],
01830 src_y + (17 << pixel_shift), 1);
01831 }
01832 }
01833 if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
01834 if (chroma444) {
01835 if (deblock_topleft) {
01836 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
01837 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
01838 }
01839 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
01840 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
01841 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
01842 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
01843 if (s->mb_x + 1 < s->mb_width) {
01844 XCHG(h->top_borders[top_idx][s->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
01845 XCHG(h->top_borders[top_idx][s->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
01846 }
01847 } else {
01848 if (deblock_top) {
01849 if (deblock_topleft) {
01850 XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
01851 XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
01852 }
01853 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
01854 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
01855 }
01856 }
01857 }
01858 }
01859
01860 static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth,
01861 int index)
01862 {
01863 if (high_bit_depth) {
01864 return AV_RN32A(((int32_t *)mb) + index);
01865 } else
01866 return AV_RN16A(mb + index);
01867 }
01868
01869 static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth,
01870 int index, int value)
01871 {
01872 if (high_bit_depth) {
01873 AV_WN32A(((int32_t *)mb) + index, value);
01874 } else
01875 AV_WN16A(mb + index, value);
01876 }
01877
01878 static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
01879 int mb_type, int is_h264,
01880 int simple,
01881 int transform_bypass,
01882 int pixel_shift,
01883 int *block_offset,
01884 int linesize,
01885 uint8_t *dest_y, int p)
01886 {
01887 MpegEncContext *const s = &h->s;
01888 void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
01889 void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
01890 int i;
01891 int qscale = p == 0 ? s->qscale : h->chroma_qp[p - 1];
01892 block_offset += 16 * p;
01893 if (IS_INTRA4x4(mb_type)) {
01894 if (simple || !s->encoding) {
01895 if (IS_8x8DCT(mb_type)) {
01896 if (transform_bypass) {
01897 idct_dc_add =
01898 idct_add = s->dsp.add_pixels8;
01899 } else {
01900 idct_dc_add = h->h264dsp.h264_idct8_dc_add;
01901 idct_add = h->h264dsp.h264_idct8_add;
01902 }
01903 for (i = 0; i < 16; i += 4) {
01904 uint8_t *const ptr = dest_y + block_offset[i];
01905 const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
01906 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
01907 h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
01908 } else {
01909 const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
01910 h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
01911 (h->topright_samples_available << i) & 0x4000, linesize);
01912 if (nnz) {
01913 if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
01914 idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
01915 else
01916 idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
01917 }
01918 }
01919 }
01920 } else {
01921 if (transform_bypass) {
01922 idct_dc_add =
01923 idct_add = s->dsp.add_pixels4;
01924 } else {
01925 idct_dc_add = h->h264dsp.h264_idct_dc_add;
01926 idct_add = h->h264dsp.h264_idct_add;
01927 }
01928 for (i = 0; i < 16; i++) {
01929 uint8_t *const ptr = dest_y + block_offset[i];
01930 const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
01931
01932 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
01933 h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
01934 } else {
01935 uint8_t *topright;
01936 int nnz, tr;
01937 uint64_t tr_high;
01938 if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
01939 const int topright_avail = (h->topright_samples_available << i) & 0x8000;
01940 assert(s->mb_y || linesize <= block_offset[i]);
01941 if (!topright_avail) {
01942 if (pixel_shift) {
01943 tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
01944 topright = (uint8_t *)&tr_high;
01945 } else {
01946 tr = ptr[3 - linesize] * 0x01010101u;
01947 topright = (uint8_t *)&tr;
01948 }
01949 } else
01950 topright = ptr + (4 << pixel_shift) - linesize;
01951 } else
01952 topright = NULL;
01953
01954 h->hpc.pred4x4[dir](ptr, topright, linesize);
01955 nnz = h->non_zero_count_cache[scan8[i + p * 16]];
01956 if (nnz) {
01957 if (is_h264) {
01958 if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
01959 idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
01960 else
01961 idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
01962 } else if (CONFIG_SVQ3_DECODER)
01963 ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
01964 }
01965 }
01966 }
01967 }
01968 }
01969 } else {
01970 h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
01971 if (is_h264) {
01972 if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
01973 if (!transform_bypass)
01974 h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
01975 h->mb_luma_dc[p],
01976 h->dequant4_coeff[p][qscale][0]);
01977 else {
01978 static const uint8_t dc_mapping[16] = {
01979 0 * 16, 1 * 16, 4 * 16, 5 * 16,
01980 2 * 16, 3 * 16, 6 * 16, 7 * 16,
01981 8 * 16, 9 * 16, 12 * 16, 13 * 16,
01982 10 * 16, 11 * 16, 14 * 16, 15 * 16 };
01983 for (i = 0; i < 16; i++)
01984 dctcoef_set(h->mb + (p * 256 << pixel_shift),
01985 pixel_shift, dc_mapping[i],
01986 dctcoef_get(h->mb_luma_dc[p],
01987 pixel_shift, i));
01988 }
01989 }
01990 } else if (CONFIG_SVQ3_DECODER)
01991 ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
01992 h->mb_luma_dc[p], qscale);
01993 }
01994 }
01995
01996 static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
01997 int is_h264, int simple,
01998 int transform_bypass,
01999 int pixel_shift,
02000 int *block_offset,
02001 int linesize,
02002 uint8_t *dest_y, int p)
02003 {
02004 MpegEncContext *const s = &h->s;
02005 void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
02006 int i;
02007 block_offset += 16 * p;
02008 if (!IS_INTRA4x4(mb_type)) {
02009 if (is_h264) {
02010 if (IS_INTRA16x16(mb_type)) {
02011 if (transform_bypass) {
02012 if (h->sps.profile_idc == 244 &&
02013 (h->intra16x16_pred_mode == VERT_PRED8x8 ||
02014 h->intra16x16_pred_mode == HOR_PRED8x8)) {
02015 h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
02016 h->mb + (p * 256 << pixel_shift),
02017 linesize);
02018 } else {
02019 for (i = 0; i < 16; i++)
02020 if (h->non_zero_count_cache[scan8[i + p * 16]] ||
02021 dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
02022 s->dsp.add_pixels4(dest_y + block_offset[i],
02023 h->mb + (i * 16 + p * 256 << pixel_shift),
02024 linesize);
02025 }
02026 } else {
02027 h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
02028 h->mb + (p * 256 << pixel_shift),
02029 linesize,
02030 h->non_zero_count_cache + p * 5 * 8);
02031 }
02032 } else if (h->cbp & 15) {
02033 if (transform_bypass) {
02034 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
02035 idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8
02036 : s->dsp.add_pixels4;
02037 for (i = 0; i < 16; i += di)
02038 if (h->non_zero_count_cache[scan8[i + p * 16]])
02039 idct_add(dest_y + block_offset[i],
02040 h->mb + (i * 16 + p * 256 << pixel_shift),
02041 linesize);
02042 } else {
02043 if (IS_8x8DCT(mb_type))
02044 h->h264dsp.h264_idct8_add4(dest_y, block_offset,
02045 h->mb + (p * 256 << pixel_shift),
02046 linesize,
02047 h->non_zero_count_cache + p * 5 * 8);
02048 else
02049 h->h264dsp.h264_idct_add16(dest_y, block_offset,
02050 h->mb + (p * 256 << pixel_shift),
02051 linesize,
02052 h->non_zero_count_cache + p * 5 * 8);
02053 }
02054 }
02055 } else if (CONFIG_SVQ3_DECODER) {
02056 for (i = 0; i < 16; i++)
02057 if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
02058
02059 uint8_t *const ptr = dest_y + block_offset[i];
02060 ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
02061 s->qscale, IS_INTRA(mb_type) ? 1 : 0);
02062 }
02063 }
02064 }
02065 }
02066
02067 static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple,
02068 int pixel_shift)
02069 {
02070 MpegEncContext *const s = &h->s;
02071 const int mb_x = s->mb_x;
02072 const int mb_y = s->mb_y;
02073 const int mb_xy = h->mb_xy;
02074 const int mb_type = s->current_picture.f.mb_type[mb_xy];
02075 uint8_t *dest_y, *dest_cb, *dest_cr;
02076 int linesize, uvlinesize ;
02077 int i, j;
02078 int *block_offset = &h->block_offset[0];
02079 const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
02080
02081 const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
02082 void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
02083 const int block_h = 16 >> s->chroma_y_shift;
02084 const int chroma422 = CHROMA422;
02085
02086 dest_y = s->current_picture.f.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
02087 dest_cb = s->current_picture.f.data[1] + (mb_x << pixel_shift) * 8 + mb_y * s->uvlinesize * block_h;
02088 dest_cr = s->current_picture.f.data[2] + (mb_x << pixel_shift) * 8 + mb_y * s->uvlinesize * block_h;
02089
02090 s->dsp.prefetch(dest_y + (s->mb_x & 3) * 4 * s->linesize + (64 << pixel_shift), s->linesize, 4);
02091 s->dsp.prefetch(dest_cb + (s->mb_x & 7) * s->uvlinesize + (64 << pixel_shift), dest_cr - dest_cb, 2);
02092
02093 h->list_counts[mb_xy] = h->list_count;
02094
02095 if (!simple && MB_FIELD) {
02096 linesize = h->mb_linesize = s->linesize * 2;
02097 uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
02098 block_offset = &h->block_offset[48];
02099 if (mb_y & 1) {
02100 dest_y -= s->linesize * 15;
02101 dest_cb -= s->uvlinesize * (block_h - 1);
02102 dest_cr -= s->uvlinesize * (block_h - 1);
02103 }
02104 if (FRAME_MBAFF) {
02105 int list;
02106 for (list = 0; list < h->list_count; list++) {
02107 if (!USES_LIST(mb_type, list))
02108 continue;
02109 if (IS_16X16(mb_type)) {
02110 int8_t *ref = &h->ref_cache[list][scan8[0]];
02111 fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
02112 } else {
02113 for (i = 0; i < 16; i += 4) {
02114 int ref = h->ref_cache[list][scan8[i]];
02115 if (ref >= 0)
02116 fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
02117 8, (16 + ref) ^ (s->mb_y & 1), 1);
02118 }
02119 }
02120 }
02121 }
02122 } else {
02123 linesize = h->mb_linesize = s->linesize;
02124 uvlinesize = h->mb_uvlinesize = s->uvlinesize;
02125
02126 }
02127
02128 if (!simple && IS_INTRA_PCM(mb_type)) {
02129 const int bit_depth = h->sps.bit_depth_luma;
02130 if (pixel_shift) {
02131 int j;
02132 GetBitContext gb;
02133 init_get_bits(&gb, (uint8_t *)h->mb,
02134 ff_h264_mb_sizes[h->sps.chroma_format_idc] * bit_depth);
02135
02136 for (i = 0; i < 16; i++) {
02137 uint16_t *tmp_y = (uint16_t *)(dest_y + i * linesize);
02138 for (j = 0; j < 16; j++)
02139 tmp_y[j] = get_bits(&gb, bit_depth);
02140 }
02141 if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
02142 if (!h->sps.chroma_format_idc) {
02143 for (i = 0; i < block_h; i++) {
02144 uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
02145 uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
02146 for (j = 0; j < 8; j++) {
02147 tmp_cb[j] = tmp_cr[j] = 1 << (bit_depth - 1);
02148 }
02149 }
02150 } else {
02151 for (i = 0; i < block_h; i++) {
02152 uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
02153 for (j = 0; j < 8; j++)
02154 tmp_cb[j] = get_bits(&gb, bit_depth);
02155 }
02156 for (i = 0; i < block_h; i++) {
02157 uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
02158 for (j = 0; j < 8; j++)
02159 tmp_cr[j] = get_bits(&gb, bit_depth);
02160 }
02161 }
02162 }
02163 } else {
02164 for (i = 0; i < 16; i++)
02165 memcpy(dest_y + i * linesize, (uint8_t *)h->mb + i * 16, 16);
02166 if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
02167 if (!h->sps.chroma_format_idc) {
02168 for (i = 0; i < 8; i++) {
02169 memset(dest_cb + i*uvlinesize, 1 << (bit_depth - 1), 8);
02170 memset(dest_cr + i*uvlinesize, 1 << (bit_depth - 1), 8);
02171 }
02172 } else {
02173 uint8_t *src_cb = (uint8_t *)h->mb + 256;
02174 uint8_t *src_cr = (uint8_t *)h->mb + 256 + block_h * 8;
02175 for (i = 0; i < block_h; i++) {
02176 memcpy(dest_cb + i * uvlinesize, src_cb + i * 8, 8);
02177 memcpy(dest_cr + i * uvlinesize, src_cr + i * 8, 8);
02178 }
02179 }
02180 }
02181 }
02182 } else {
02183 if (IS_INTRA(mb_type)) {
02184 if (h->deblocking_filter)
02185 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
02186 uvlinesize, 1, 0, simple, pixel_shift);
02187
02188 if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
02189 if (CHROMA) {
02190 h->hpc.pred8x8[h->chroma_pred_mode](dest_cb, uvlinesize);
02191 h->hpc.pred8x8[h->chroma_pred_mode](dest_cr, uvlinesize);
02192 }
02193 }
02194
02195 hl_decode_mb_predict_luma(h, mb_type, is_h264, simple,
02196 transform_bypass, pixel_shift,
02197 block_offset, linesize, dest_y, 0);
02198
02199 if (h->deblocking_filter)
02200 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
02201 uvlinesize, 0, 0, simple, pixel_shift);
02202 } else if (is_h264) {
02203 if (chroma422) {
02204 hl_motion_422(h, dest_y, dest_cb, dest_cr,
02205 s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
02206 s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
02207 h->h264dsp.weight_h264_pixels_tab,
02208 h->h264dsp.biweight_h264_pixels_tab,
02209 pixel_shift);
02210 } else {
02211 hl_motion_420(h, dest_y, dest_cb, dest_cr,
02212 s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
02213 s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
02214 h->h264dsp.weight_h264_pixels_tab,
02215 h->h264dsp.biweight_h264_pixels_tab,
02216 pixel_shift);
02217 }
02218 }
02219
02220 hl_decode_mb_idct_luma(h, mb_type, is_h264, simple, transform_bypass,
02221 pixel_shift, block_offset, linesize, dest_y, 0);
02222
02223 if ((simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) &&
02224 (h->cbp & 0x30)) {
02225 uint8_t *dest[2] = { dest_cb, dest_cr };
02226 if (transform_bypass) {
02227 if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
02228 (h->chroma_pred_mode == VERT_PRED8x8 ||
02229 h->chroma_pred_mode == HOR_PRED8x8)) {
02230 h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0],
02231 block_offset + 16,
02232 h->mb + (16 * 16 * 1 << pixel_shift),
02233 uvlinesize);
02234 h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1],
02235 block_offset + 32,
02236 h->mb + (16 * 16 * 2 << pixel_shift),
02237 uvlinesize);
02238 } else {
02239 idct_add = s->dsp.add_pixels4;
02240 for (j = 1; j < 3; j++) {
02241 for (i = j * 16; i < j * 16 + 4; i++)
02242 if (h->non_zero_count_cache[scan8[i]] ||
02243 dctcoef_get(h->mb, pixel_shift, i * 16))
02244 idct_add(dest[j - 1] + block_offset[i],
02245 h->mb + (i * 16 << pixel_shift),
02246 uvlinesize);
02247 if (chroma422) {
02248 for (i = j * 16 + 4; i < j * 16 + 8; i++)
02249 if (h->non_zero_count_cache[scan8[i + 4]] ||
02250 dctcoef_get(h->mb, pixel_shift, i * 16))
02251 idct_add(dest[j - 1] + block_offset[i + 4],
02252 h->mb + (i * 16 << pixel_shift),
02253 uvlinesize);
02254 }
02255 }
02256 }
02257 } else {
02258 if (is_h264) {
02259 int qp[2];
02260 if (chroma422) {
02261 qp[0] = h->chroma_qp[0] + 3;
02262 qp[1] = h->chroma_qp[1] + 3;
02263 } else {
02264 qp[0] = h->chroma_qp[0];
02265 qp[1] = h->chroma_qp[1];
02266 }
02267 if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
02268 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 1 << pixel_shift),
02269 h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
02270 if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
02271 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 2 << pixel_shift),
02272 h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][qp[1]][0]);
02273 h->h264dsp.h264_idct_add8(dest, block_offset,
02274 h->mb, uvlinesize,
02275 h->non_zero_count_cache);
02276 } else if (CONFIG_SVQ3_DECODER) {
02277 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 1,
02278 h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][h->chroma_qp[0]][0]);
02279 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 2,
02280 h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][h->chroma_qp[1]][0]);
02281 for (j = 1; j < 3; j++) {
02282 for (i = j * 16; i < j * 16 + 4; i++)
02283 if (h->non_zero_count_cache[scan8[i]] || h->mb[i * 16]) {
02284 uint8_t *const ptr = dest[j - 1] + block_offset[i];
02285 ff_svq3_add_idct_c(ptr, h->mb + i * 16,
02286 uvlinesize,
02287 ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
02288 }
02289 }
02290 }
02291 }
02292 }
02293 }
02294 if (h->cbp || IS_INTRA(mb_type)) {
02295 s->dsp.clear_blocks(h->mb);
02296 s->dsp.clear_blocks(h->mb + (24 * 16 << pixel_shift));
02297 }
02298 }
02299
02300 static av_always_inline void hl_decode_mb_444_internal(H264Context *h,
02301 int simple,
02302 int pixel_shift)
02303 {
02304 MpegEncContext *const s = &h->s;
02305 const int mb_x = s->mb_x;
02306 const int mb_y = s->mb_y;
02307 const int mb_xy = h->mb_xy;
02308 const int mb_type = s->current_picture.f.mb_type[mb_xy];
02309 uint8_t *dest[3];
02310 int linesize;
02311 int i, j, p;
02312 int *block_offset = &h->block_offset[0];
02313 const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
02314 const int plane_count = (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) ? 3 : 1;
02315
02316 for (p = 0; p < plane_count; p++) {
02317 dest[p] = s->current_picture.f.data[p] +
02318 ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
02319 s->dsp.prefetch(dest[p] + (s->mb_x & 3) * 4 * s->linesize + (64 << pixel_shift),
02320 s->linesize, 4);
02321 }
02322
02323 h->list_counts[mb_xy] = h->list_count;
02324
02325 if (!simple && MB_FIELD) {
02326 linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
02327 block_offset = &h->block_offset[48];
02328 if (mb_y & 1)
02329 for (p = 0; p < 3; p++)
02330 dest[p] -= s->linesize * 15;
02331 if (FRAME_MBAFF) {
02332 int list;
02333 for (list = 0; list < h->list_count; list++) {
02334 if (!USES_LIST(mb_type, list))
02335 continue;
02336 if (IS_16X16(mb_type)) {
02337 int8_t *ref = &h->ref_cache[list][scan8[0]];
02338 fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
02339 } else {
02340 for (i = 0; i < 16; i += 4) {
02341 int ref = h->ref_cache[list][scan8[i]];
02342 if (ref >= 0)
02343 fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
02344 8, (16 + ref) ^ (s->mb_y & 1), 1);
02345 }
02346 }
02347 }
02348 }
02349 } else {
02350 linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;
02351 }
02352
02353 if (!simple && IS_INTRA_PCM(mb_type)) {
02354 if (pixel_shift) {
02355 const int bit_depth = h->sps.bit_depth_luma;
02356 GetBitContext gb;
02357 init_get_bits(&gb, (uint8_t *)h->mb, 768 * bit_depth);
02358
02359 for (p = 0; p < plane_count; p++)
02360 for (i = 0; i < 16; i++) {
02361 uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
02362 for (j = 0; j < 16; j++)
02363 tmp[j] = get_bits(&gb, bit_depth);
02364 }
02365 } else {
02366 for (p = 0; p < plane_count; p++)
02367 for (i = 0; i < 16; i++)
02368 memcpy(dest[p] + i * linesize,
02369 (uint8_t *)h->mb + p * 256 + i * 16, 16);
02370 }
02371 } else {
02372 if (IS_INTRA(mb_type)) {
02373 if (h->deblocking_filter)
02374 xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
02375 linesize, 1, 1, simple, pixel_shift);
02376
02377 for (p = 0; p < plane_count; p++)
02378 hl_decode_mb_predict_luma(h, mb_type, 1, simple,
02379 transform_bypass, pixel_shift,
02380 block_offset, linesize, dest[p], p);
02381
02382 if (h->deblocking_filter)
02383 xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
02384 linesize, 0, 1, simple, pixel_shift);
02385 } else {
02386 hl_motion(h, dest[0], dest[1], dest[2],
02387 s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
02388 s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
02389 h->h264dsp.weight_h264_pixels_tab,
02390 h->h264dsp.biweight_h264_pixels_tab, pixel_shift, 3);
02391 }
02392
02393 for (p = 0; p < plane_count; p++)
02394 hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass,
02395 pixel_shift, block_offset, linesize,
02396 dest[p], p);
02397 }
02398 if (h->cbp || IS_INTRA(mb_type)) {
02399 s->dsp.clear_blocks(h->mb);
02400 s->dsp.clear_blocks(h->mb + (24 * 16 << pixel_shift));
02401 }
02402 }
02403
02407 #define hl_decode_mb_simple(sh, bits) \
02408 static void hl_decode_mb_simple_ ## bits(H264Context *h) \
02409 { \
02410 hl_decode_mb_internal(h, 1, sh); \
02411 }
02412
02413 hl_decode_mb_simple(0, 8)
02414 hl_decode_mb_simple(1, 16)
02415
02419 static av_noinline void hl_decode_mb_complex(H264Context *h)
02420 {
02421 hl_decode_mb_internal(h, 0, h->pixel_shift);
02422 }
02423
02424 static av_noinline void hl_decode_mb_444_complex(H264Context *h)
02425 {
02426 hl_decode_mb_444_internal(h, 0, h->pixel_shift);
02427 }
02428
02429 static av_noinline void hl_decode_mb_444_simple(H264Context *h)
02430 {
02431 hl_decode_mb_444_internal(h, 1, 0);
02432 }
02433
02434 void ff_h264_hl_decode_mb(H264Context *h)
02435 {
02436 MpegEncContext *const s = &h->s;
02437 const int mb_xy = h->mb_xy;
02438 const int mb_type = s->current_picture.f.mb_type[mb_xy];
02439 int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
02440
02441 if (CHROMA444) {
02442 if (is_complex || h->pixel_shift)
02443 hl_decode_mb_444_complex(h);
02444 else
02445 hl_decode_mb_444_simple(h);
02446 } else if (is_complex) {
02447 hl_decode_mb_complex(h);
02448 } else if (h->pixel_shift) {
02449 hl_decode_mb_simple_16(h);
02450 } else
02451 hl_decode_mb_simple_8(h);
02452 }
02453
02454 static int pred_weight_table(H264Context *h)
02455 {
02456 MpegEncContext *const s = &h->s;
02457 int list, i;
02458 int luma_def, chroma_def;
02459
02460 h->use_weight = 0;
02461 h->use_weight_chroma = 0;
02462 h->luma_log2_weight_denom = get_ue_golomb(&s->gb);
02463 if (h->sps.chroma_format_idc)
02464 h->chroma_log2_weight_denom = get_ue_golomb(&s->gb);
02465 luma_def = 1 << h->luma_log2_weight_denom;
02466 chroma_def = 1 << h->chroma_log2_weight_denom;
02467
02468 for (list = 0; list < 2; list++) {
02469 h->luma_weight_flag[list] = 0;
02470 h->chroma_weight_flag[list] = 0;
02471 for (i = 0; i < h->ref_count[list]; i++) {
02472 int luma_weight_flag, chroma_weight_flag;
02473
02474 luma_weight_flag = get_bits1(&s->gb);
02475 if (luma_weight_flag) {
02476 h->luma_weight[i][list][0] = get_se_golomb(&s->gb);
02477 h->luma_weight[i][list][1] = get_se_golomb(&s->gb);
02478 if (h->luma_weight[i][list][0] != luma_def ||
02479 h->luma_weight[i][list][1] != 0) {
02480 h->use_weight = 1;
02481 h->luma_weight_flag[list] = 1;
02482 }
02483 } else {
02484 h->luma_weight[i][list][0] = luma_def;
02485 h->luma_weight[i][list][1] = 0;
02486 }
02487
02488 if (h->sps.chroma_format_idc) {
02489 chroma_weight_flag = get_bits1(&s->gb);
02490 if (chroma_weight_flag) {
02491 int j;
02492 for (j = 0; j < 2; j++) {
02493 h->chroma_weight[i][list][j][0] = get_se_golomb(&s->gb);
02494 h->chroma_weight[i][list][j][1] = get_se_golomb(&s->gb);
02495 if (h->chroma_weight[i][list][j][0] != chroma_def ||
02496 h->chroma_weight[i][list][j][1] != 0) {
02497 h->use_weight_chroma = 1;
02498 h->chroma_weight_flag[list] = 1;
02499 }
02500 }
02501 } else {
02502 int j;
02503 for (j = 0; j < 2; j++) {
02504 h->chroma_weight[i][list][j][0] = chroma_def;
02505 h->chroma_weight[i][list][j][1] = 0;
02506 }
02507 }
02508 }
02509 }
02510 if (h->slice_type_nos != AV_PICTURE_TYPE_B)
02511 break;
02512 }
02513 h->use_weight = h->use_weight || h->use_weight_chroma;
02514 return 0;
02515 }
02516
02522 static void implicit_weight_table(H264Context *h, int field)
02523 {
02524 MpegEncContext *const s = &h->s;
02525 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
02526
02527 for (i = 0; i < 2; i++) {
02528 h->luma_weight_flag[i] = 0;
02529 h->chroma_weight_flag[i] = 0;
02530 }
02531
02532 if (field < 0) {
02533 if (s->picture_structure == PICT_FRAME) {
02534 cur_poc = s->current_picture_ptr->poc;
02535 } else {
02536 cur_poc = s->current_picture_ptr->field_poc[s->picture_structure - 1];
02537 }
02538 if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF &&
02539 h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
02540 h->use_weight = 0;
02541 h->use_weight_chroma = 0;
02542 return;
02543 }
02544 ref_start = 0;
02545 ref_count0 = h->ref_count[0];
02546 ref_count1 = h->ref_count[1];
02547 } else {
02548 cur_poc = s->current_picture_ptr->field_poc[field];
02549 ref_start = 16;
02550 ref_count0 = 16 + 2 * h->ref_count[0];
02551 ref_count1 = 16 + 2 * h->ref_count[1];
02552 }
02553
02554 h->use_weight = 2;
02555 h->use_weight_chroma = 2;
02556 h->luma_log2_weight_denom = 5;
02557 h->chroma_log2_weight_denom = 5;
02558
02559 for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
02560 int poc0 = h->ref_list[0][ref0].poc;
02561 for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
02562 int w = 32;
02563 if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
02564 int poc1 = h->ref_list[1][ref1].poc;
02565 int td = av_clip(poc1 - poc0, -128, 127);
02566 if (td) {
02567 int tb = av_clip(cur_poc - poc0, -128, 127);
02568 int tx = (16384 + (FFABS(td) >> 1)) / td;
02569 int dist_scale_factor = (tb * tx + 32) >> 8;
02570 if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
02571 w = 64 - dist_scale_factor;
02572 }
02573 }
02574 if (field < 0) {
02575 h->implicit_weight[ref0][ref1][0] =
02576 h->implicit_weight[ref0][ref1][1] = w;
02577 } else {
02578 h->implicit_weight[ref0][ref1][field] = w;
02579 }
02580 }
02581 }
02582 }
02583
02587 static void idr(H264Context *h)
02588 {
02589 int i;
02590 ff_h264_remove_all_refs(h);
02591 h->prev_frame_num = 0;
02592 h->prev_frame_num_offset = 0;
02593 h->prev_poc_msb = 1<<16;
02594 h->prev_poc_lsb = 0;
02595 for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
02596 h->last_pocs[i] = INT_MIN;
02597 }
02598
02599
02600 static void flush_dpb(AVCodecContext *avctx)
02601 {
02602 H264Context *h = avctx->priv_data;
02603 int i;
02604 for (i=0; i<=MAX_DELAYED_PIC_COUNT; i++) {
02605 if (h->delayed_pic[i])
02606 h->delayed_pic[i]->f.reference = 0;
02607 h->delayed_pic[i] = NULL;
02608 }
02609 h->outputed_poc = h->next_outputed_poc = INT_MIN;
02610 h->prev_interlaced_frame = 1;
02611 idr(h);
02612 h->prev_frame_num = -1;
02613 if (h->s.current_picture_ptr)
02614 h->s.current_picture_ptr->f.reference = 0;
02615 h->s.first_field = 0;
02616 ff_h264_reset_sei(h);
02617 ff_mpeg_flush(avctx);
02618 h->recovery_frame= -1;
02619 h->sync= 0;
02620 }
02621
02622 static int init_poc(H264Context *h)
02623 {
02624 MpegEncContext *const s = &h->s;
02625 const int max_frame_num = 1 << h->sps.log2_max_frame_num;
02626 int field_poc[2];
02627 Picture *cur = s->current_picture_ptr;
02628
02629 h->frame_num_offset = h->prev_frame_num_offset;
02630 if (h->frame_num < h->prev_frame_num)
02631 h->frame_num_offset += max_frame_num;
02632
02633 if (h->sps.poc_type == 0) {
02634 const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
02635
02636 if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
02637 h->poc_msb = h->prev_poc_msb + max_poc_lsb;
02638 else if (h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
02639 h->poc_msb = h->prev_poc_msb - max_poc_lsb;
02640 else
02641 h->poc_msb = h->prev_poc_msb;
02642
02643 field_poc[0] =
02644 field_poc[1] = h->poc_msb + h->poc_lsb;
02645 if (s->picture_structure == PICT_FRAME)
02646 field_poc[1] += h->delta_poc_bottom;
02647 } else if (h->sps.poc_type == 1) {
02648 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
02649 int i;
02650
02651 if (h->sps.poc_cycle_length != 0)
02652 abs_frame_num = h->frame_num_offset + h->frame_num;
02653 else
02654 abs_frame_num = 0;
02655
02656 if (h->nal_ref_idc == 0 && abs_frame_num > 0)
02657 abs_frame_num--;
02658
02659 expected_delta_per_poc_cycle = 0;
02660 for (i = 0; i < h->sps.poc_cycle_length; i++)
02661
02662 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
02663
02664 if (abs_frame_num > 0) {
02665 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
02666 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
02667
02668 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
02669 for (i = 0; i <= frame_num_in_poc_cycle; i++)
02670 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
02671 } else
02672 expectedpoc = 0;
02673
02674 if (h->nal_ref_idc == 0)
02675 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
02676
02677 field_poc[0] = expectedpoc + h->delta_poc[0];
02678 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
02679
02680 if (s->picture_structure == PICT_FRAME)
02681 field_poc[1] += h->delta_poc[1];
02682 } else {
02683 int poc = 2 * (h->frame_num_offset + h->frame_num);
02684
02685 if (!h->nal_ref_idc)
02686 poc--;
02687
02688 field_poc[0] = poc;
02689 field_poc[1] = poc;
02690 }
02691
02692 if (s->picture_structure != PICT_BOTTOM_FIELD)
02693 s->current_picture_ptr->field_poc[0] = field_poc[0];
02694 if (s->picture_structure != PICT_TOP_FIELD)
02695 s->current_picture_ptr->field_poc[1] = field_poc[1];
02696 cur->poc = FFMIN(cur->field_poc[0], cur->field_poc[1]);
02697
02698 return 0;
02699 }
02700
02704 static void init_scan_tables(H264Context *h)
02705 {
02706 int i;
02707 for (i = 0; i < 16; i++) {
02708 #define T(x) (x >> 2) | ((x << 2) & 0xF)
02709 h->zigzag_scan[i] = T(zigzag_scan[i]);
02710 h->field_scan[i] = T(field_scan[i]);
02711 #undef T
02712 }
02713 for (i = 0; i < 64; i++) {
02714 #define T(x) (x >> 3) | ((x & 7) << 3)
02715 h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
02716 h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
02717 h->field_scan8x8[i] = T(field_scan8x8[i]);
02718 h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
02719 #undef T
02720 }
02721 if (h->sps.transform_bypass) {
02722 memcpy(h->zigzag_scan_q0 , zigzag_scan , sizeof(h->zigzag_scan_q0 ));
02723 memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
02724 memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
02725 memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
02726 memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
02727 memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
02728 } else {
02729 memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
02730 memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
02731 memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
02732 memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
02733 memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
02734 memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
02735 }
02736 }
02737
02738 static int field_end(H264Context *h, int in_setup)
02739 {
02740 MpegEncContext *const s = &h->s;
02741 AVCodecContext *const avctx = s->avctx;
02742 int err = 0;
02743 s->mb_y = 0;
02744
02745 if (!in_setup && !s->dropable)
02746 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
02747 s->picture_structure == PICT_BOTTOM_FIELD);
02748
02749 if (CONFIG_H264_VDPAU_DECODER &&
02750 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
02751 ff_vdpau_h264_set_reference_frames(s);
02752
02753 if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
02754 if (!s->dropable) {
02755 err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
02756 h->prev_poc_msb = h->poc_msb;
02757 h->prev_poc_lsb = h->poc_lsb;
02758 }
02759 h->prev_frame_num_offset = h->frame_num_offset;
02760 h->prev_frame_num = h->frame_num;
02761 h->outputed_poc = h->next_outputed_poc;
02762 }
02763
02764 if (avctx->hwaccel) {
02765 if (avctx->hwaccel->end_frame(avctx) < 0)
02766 av_log(avctx, AV_LOG_ERROR,
02767 "hardware accelerator failed to decode picture\n");
02768 }
02769
02770 if (CONFIG_H264_VDPAU_DECODER &&
02771 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
02772 ff_vdpau_h264_picture_complete(s);
02773
02774
02775
02776
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786 if (!FIELD_PICTURE)
02787 ff_er_frame_end(s);
02788
02789 ff_MPV_frame_end(s);
02790
02791 h->current_slice = 0;
02792
02793 return err;
02794 }
02795
02799 static void clone_slice(H264Context *dst, H264Context *src)
02800 {
02801 memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
02802 dst->s.current_picture_ptr = src->s.current_picture_ptr;
02803 dst->s.current_picture = src->s.current_picture;
02804 dst->s.linesize = src->s.linesize;
02805 dst->s.uvlinesize = src->s.uvlinesize;
02806 dst->s.first_field = src->s.first_field;
02807
02808 dst->prev_poc_msb = src->prev_poc_msb;
02809 dst->prev_poc_lsb = src->prev_poc_lsb;
02810 dst->prev_frame_num_offset = src->prev_frame_num_offset;
02811 dst->prev_frame_num = src->prev_frame_num;
02812 dst->short_ref_count = src->short_ref_count;
02813
02814 memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
02815 memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
02816 memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
02817 memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
02818
02819 memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
02820 memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
02821 }
02822
02830 int ff_h264_get_profile(SPS *sps)
02831 {
02832 int profile = sps->profile_idc;
02833
02834 switch (sps->profile_idc) {
02835 case FF_PROFILE_H264_BASELINE:
02836
02837 profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
02838 break;
02839 case FF_PROFILE_H264_HIGH_10:
02840 case FF_PROFILE_H264_HIGH_422:
02841 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
02842
02843 profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
02844 break;
02845 }
02846
02847 return profile;
02848 }
02849
02860 static int decode_slice_header(H264Context *h, H264Context *h0)
02861 {
02862 MpegEncContext *const s = &h->s;
02863 MpegEncContext *const s0 = &h0->s;
02864 unsigned int first_mb_in_slice;
02865 unsigned int pps_id;
02866 int num_ref_idx_active_override_flag;
02867 unsigned int slice_type, tmp, i, j;
02868 int default_ref_list_done = 0;
02869 int last_pic_structure, last_pic_dropable;
02870 int must_reinit;
02871
02872
02873 if ((s->avctx->flags2 & CODEC_FLAG2_FAST) &&
02874 !h->nal_ref_idc && !h->pixel_shift) {
02875 s->me.qpel_put = s->dsp.put_2tap_qpel_pixels_tab;
02876 s->me.qpel_avg = s->dsp.avg_2tap_qpel_pixels_tab;
02877 } else {
02878 s->me.qpel_put = s->dsp.put_h264_qpel_pixels_tab;
02879 s->me.qpel_avg = s->dsp.avg_h264_qpel_pixels_tab;
02880 }
02881
02882 first_mb_in_slice = get_ue_golomb_long(&s->gb);
02883
02884 if (first_mb_in_slice == 0) {
02885 if (h0->current_slice && FIELD_PICTURE) {
02886 field_end(h, 1);
02887 }
02888
02889 h0->current_slice = 0;
02890 if (!s0->first_field) {
02891 if (s->current_picture_ptr && !s->dropable &&
02892 s->current_picture_ptr->owner2 == s) {
02893 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
02894 s->picture_structure == PICT_BOTTOM_FIELD);
02895 }
02896 s->current_picture_ptr = NULL;
02897 }
02898 }
02899
02900 slice_type = get_ue_golomb_31(&s->gb);
02901 if (slice_type > 9) {
02902 av_log(h->s.avctx, AV_LOG_ERROR,
02903 "slice type too large (%d) at %d %d\n",
02904 h->slice_type, s->mb_x, s->mb_y);
02905 return -1;
02906 }
02907 if (slice_type > 4) {
02908 slice_type -= 5;
02909 h->slice_type_fixed = 1;
02910 } else
02911 h->slice_type_fixed = 0;
02912
02913 slice_type = golomb_to_pict_type[slice_type];
02914 if (slice_type == AV_PICTURE_TYPE_I ||
02915 (h0->current_slice != 0 && slice_type == h0->last_slice_type)) {
02916 default_ref_list_done = 1;
02917 }
02918 h->slice_type = slice_type;
02919 h->slice_type_nos = slice_type & 3;
02920
02921
02922 s->pict_type = h->slice_type;
02923
02924 pps_id = get_ue_golomb(&s->gb);
02925 if (pps_id >= MAX_PPS_COUNT) {
02926 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id %d out of range\n", pps_id);
02927 return -1;
02928 }
02929 if (!h0->pps_buffers[pps_id]) {
02930 av_log(h->s.avctx, AV_LOG_ERROR,
02931 "non-existing PPS %u referenced\n",
02932 pps_id);
02933 return -1;
02934 }
02935 h->pps = *h0->pps_buffers[pps_id];
02936
02937 if (!h0->sps_buffers[h->pps.sps_id]) {
02938 av_log(h->s.avctx, AV_LOG_ERROR,
02939 "non-existing SPS %u referenced\n",
02940 h->pps.sps_id);
02941 return -1;
02942 }
02943 h->sps = *h0->sps_buffers[h->pps.sps_id];
02944
02945 s->avctx->profile = ff_h264_get_profile(&h->sps);
02946 s->avctx->level = h->sps.level_idc;
02947 s->avctx->refs = h->sps.ref_frame_count;
02948
02949 must_reinit = (s->context_initialized &&
02950 ( 16*h->sps.mb_width != s->avctx->coded_width
02951 || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != s->avctx->coded_height
02952 || s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma
02953 || h->cur_chroma_format_idc != h->sps.chroma_format_idc
02954 || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio)));
02955
02956 if(must_reinit && (h != h0 || (s->avctx->active_thread_type & FF_THREAD_FRAME))) {
02957 av_log_missing_feature(s->avctx,
02958 "Width/height/bit depth/chroma idc changing with threads is", 0);
02959 return AVERROR_PATCHWELCOME;
02960 }
02961
02962 s->mb_width = h->sps.mb_width;
02963 s->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
02964
02965 h->b_stride = s->mb_width * 4;
02966
02967 s->chroma_y_shift = h->sps.chroma_format_idc <= 1;
02968
02969 s->width = 16 * s->mb_width;
02970 s->height = 16 * s->mb_height;
02971
02972
02973 if(must_reinit) {
02974 free_tables(h, 0);
02975 flush_dpb(s->avctx);
02976 ff_MPV_common_end(s);
02977 h->list_count = 0;
02978 h->current_slice = 0;
02979 }
02980 if (!s->context_initialized) {
02981 if (h != h0) {
02982 av_log(h->s.avctx, AV_LOG_ERROR,
02983 "Cannot (re-)initialize context during parallel decoding.\n");
02984 return -1;
02985 }
02986 avcodec_set_dimensions(s->avctx, s->width, s->height);
02987 s->avctx->width -= (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
02988 s->avctx->height -= (1<<s->chroma_y_shift)*FFMIN(h->sps.crop_bottom, (16>>s->chroma_y_shift)-1) * (2 - h->sps.frame_mbs_only_flag);
02989 s->avctx->sample_aspect_ratio = h->sps.sar;
02990 av_assert0(s->avctx->sample_aspect_ratio.den);
02991
02992 if (s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
02993 h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
02994 if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10 &&
02995 (h->sps.bit_depth_luma != 9 || !CHROMA422)) {
02996 s->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
02997 h->cur_chroma_format_idc = h->sps.chroma_format_idc;
02998 h->pixel_shift = h->sps.bit_depth_luma > 8;
02999
03000 ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
03001 ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
03002 s->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
03003 ff_dsputil_init(&s->dsp, s->avctx);
03004 } else {
03005 av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d chroma_idc: %d\n",
03006 h->sps.bit_depth_luma, h->sps.chroma_format_idc);
03007 return -1;
03008 }
03009 }
03010
03011 if (h->sps.video_signal_type_present_flag) {
03012 s->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
03013 : AVCOL_RANGE_MPEG;
03014 if (h->sps.colour_description_present_flag) {
03015 s->avctx->color_primaries = h->sps.color_primaries;
03016 s->avctx->color_trc = h->sps.color_trc;
03017 s->avctx->colorspace = h->sps.colorspace;
03018 }
03019 }
03020
03021 if (h->sps.timing_info_present_flag) {
03022 int64_t den = h->sps.time_scale;
03023 if (h->x264_build < 44U)
03024 den *= 2;
03025 av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
03026 h->sps.num_units_in_tick, den, 1 << 30);
03027 }
03028
03029 switch (h->sps.bit_depth_luma) {
03030 case 9:
03031 if (CHROMA444) {
03032 if (s->avctx->colorspace == AVCOL_SPC_RGB) {
03033 s->avctx->pix_fmt = PIX_FMT_GBRP9;
03034 } else
03035 s->avctx->pix_fmt = PIX_FMT_YUV444P9;
03036 } else if (CHROMA422)
03037 s->avctx->pix_fmt = PIX_FMT_YUV422P9;
03038 else
03039 s->avctx->pix_fmt = PIX_FMT_YUV420P9;
03040 break;
03041 case 10:
03042 if (CHROMA444) {
03043 if (s->avctx->colorspace == AVCOL_SPC_RGB) {
03044 s->avctx->pix_fmt = PIX_FMT_GBRP10;
03045 } else
03046 s->avctx->pix_fmt = PIX_FMT_YUV444P10;
03047 } else if (CHROMA422)
03048 s->avctx->pix_fmt = PIX_FMT_YUV422P10;
03049 else
03050 s->avctx->pix_fmt = PIX_FMT_YUV420P10;
03051 break;
03052 case 8:
03053 if (CHROMA444) {
03054 s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P
03055 : PIX_FMT_YUV444P;
03056 if (s->avctx->colorspace == AVCOL_SPC_RGB) {
03057 s->avctx->pix_fmt = PIX_FMT_GBR24P;
03058 av_log(h->s.avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
03059 } else if (s->avctx->colorspace == AVCOL_SPC_YCGCO) {
03060 av_log(h->s.avctx, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
03061 }
03062 } else if (CHROMA422) {
03063 s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ422P
03064 : PIX_FMT_YUV422P;
03065 } else {
03066 s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
03067 s->avctx->codec->pix_fmts ?
03068 s->avctx->codec->pix_fmts :
03069 s->avctx->color_range == AVCOL_RANGE_JPEG ?
03070 hwaccel_pixfmt_list_h264_jpeg_420 :
03071 ff_hwaccel_pixfmt_list_420);
03072 }
03073 break;
03074 default:
03075 av_log(s->avctx, AV_LOG_ERROR,
03076 "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
03077 return AVERROR_INVALIDDATA;
03078 }
03079
03080 s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id,
03081 s->avctx->pix_fmt);
03082
03083 if (ff_MPV_common_init(s) < 0) {
03084 av_log(h->s.avctx, AV_LOG_ERROR, "ff_MPV_common_init() failed.\n");
03085 return -1;
03086 }
03087 s->first_field = 0;
03088 h->prev_interlaced_frame = 1;
03089
03090 init_scan_tables(h);
03091 if (ff_h264_alloc_tables(h) < 0) {
03092 av_log(h->s.avctx, AV_LOG_ERROR,
03093 "Could not allocate memory for h264\n");
03094 return AVERROR(ENOMEM);
03095 }
03096
03097 if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_SLICE)) {
03098 if (context_init(h) < 0) {
03099 av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
03100 return -1;
03101 }
03102 } else {
03103 for (i = 1; i < s->slice_context_count; i++) {
03104 H264Context *c;
03105 c = h->thread_context[i] = av_malloc(sizeof(H264Context));
03106 memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
03107 memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
03108 c->h264dsp = h->h264dsp;
03109 c->sps = h->sps;
03110 c->pps = h->pps;
03111 c->pixel_shift = h->pixel_shift;
03112 c->cur_chroma_format_idc = h->cur_chroma_format_idc;
03113 init_scan_tables(c);
03114 clone_tables(c, h, i);
03115 }
03116
03117 for (i = 0; i < s->slice_context_count; i++)
03118 if (context_init(h->thread_context[i]) < 0) {
03119 av_log(h->s.avctx, AV_LOG_ERROR,
03120 "context_init() failed.\n");
03121 return -1;
03122 }
03123 }
03124 }
03125
03126 if (h == h0 && h->dequant_coeff_pps != pps_id) {
03127 h->dequant_coeff_pps = pps_id;
03128 init_dequant_tables(h);
03129 }
03130
03131 h->frame_num = get_bits(&s->gb, h->sps.log2_max_frame_num);
03132
03133 h->mb_mbaff = 0;
03134 h->mb_aff_frame = 0;
03135 last_pic_structure = s0->picture_structure;
03136 last_pic_dropable = s->dropable;
03137 s->dropable = h->nal_ref_idc == 0;
03138 if (h->sps.frame_mbs_only_flag) {
03139 s->picture_structure = PICT_FRAME;
03140 } else {
03141 if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
03142 av_log(h->s.avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
03143 return -1;
03144 }
03145 if (get_bits1(&s->gb)) {
03146 s->picture_structure = PICT_TOP_FIELD + get_bits1(&s->gb);
03147 } else {
03148 s->picture_structure = PICT_FRAME;
03149 h->mb_aff_frame = h->sps.mb_aff;
03150 }
03151 }
03152 h->mb_field_decoding_flag = s->picture_structure != PICT_FRAME;
03153
03154 if (h0->current_slice != 0) {
03155 if (last_pic_structure != s->picture_structure ||
03156 last_pic_dropable != s->dropable) {
03157 av_log(h->s.avctx, AV_LOG_ERROR,
03158 "Changing field mode (%d -> %d) between slices is not allowed\n",
03159 last_pic_structure, s->picture_structure);
03160 s->picture_structure = last_pic_structure;
03161 s->dropable = last_pic_dropable;
03162 return AVERROR_INVALIDDATA;
03163 }
03164 } else {
03165
03166
03167 if (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0) {
03168 int unwrap_prev_frame_num = h->prev_frame_num;
03169 int max_frame_num = 1 << h->sps.log2_max_frame_num;
03170
03171 if (unwrap_prev_frame_num > h->frame_num)
03172 unwrap_prev_frame_num -= max_frame_num;
03173
03174 if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
03175 unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
03176 if (unwrap_prev_frame_num < 0)
03177 unwrap_prev_frame_num += max_frame_num;
03178
03179 h->prev_frame_num = unwrap_prev_frame_num;
03180 }
03181 }
03182
03183
03184
03185
03186
03187
03188 if (s0->first_field) {
03189 assert(s0->current_picture_ptr);
03190 assert(s0->current_picture_ptr->f.data[0]);
03191 assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
03192
03193
03194 if (!last_pic_dropable && s0->current_picture_ptr->owner2 == s0) {
03195 ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
03196 last_pic_structure == PICT_BOTTOM_FIELD);
03197 }
03198
03199
03200 if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
03201
03202
03203 if (!last_pic_dropable && last_pic_structure != PICT_FRAME) {
03204 ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
03205 last_pic_structure == PICT_TOP_FIELD);
03206 }
03207 } else {
03208 if (s0->current_picture_ptr->frame_num != h->frame_num) {
03209
03210
03211
03212
03213 if (!last_pic_dropable && last_pic_structure != PICT_FRAME) {
03214 ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
03215 last_pic_structure == PICT_TOP_FIELD);
03216 }
03217 } else {
03218
03219 if (!((last_pic_structure == PICT_TOP_FIELD &&
03220 s->picture_structure == PICT_BOTTOM_FIELD) ||
03221 (last_pic_structure == PICT_BOTTOM_FIELD &&
03222 s->picture_structure == PICT_TOP_FIELD))) {
03223 av_log(s->avctx, AV_LOG_ERROR,
03224 "Invalid field mode combination %d/%d\n",
03225 last_pic_structure, s->picture_structure);
03226 s->picture_structure = last_pic_structure;
03227 s->dropable = last_pic_dropable;
03228 return AVERROR_INVALIDDATA;
03229 } else if (last_pic_dropable != s->dropable) {
03230 av_log(s->avctx, AV_LOG_ERROR,
03231 "Cannot combine reference and non-reference fields in the same frame\n");
03232 av_log_ask_for_sample(s->avctx, NULL);
03233 s->picture_structure = last_pic_structure;
03234 s->dropable = last_pic_dropable;
03235 return AVERROR_INVALIDDATA;
03236 }
03237
03238
03239
03240
03241
03242
03243
03244 s0->current_picture_ptr->owner2 = s0;
03245 }
03246 }
03247 }
03248
03249 while (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0 &&
03250 h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
03251 Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
03252 av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
03253 h->frame_num, h->prev_frame_num);
03254 if (ff_h264_frame_start(h) < 0)
03255 return -1;
03256 h->prev_frame_num++;
03257 h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
03258 s->current_picture_ptr->frame_num = h->prev_frame_num;
03259 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
03260 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 1);
03261 ff_generate_sliding_window_mmcos(h);
03262 if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
03263 (s->avctx->err_recognition & AV_EF_EXPLODE))
03264 return AVERROR_INVALIDDATA;
03265
03266
03267
03268
03269
03270
03271 if (h->short_ref_count) {
03272 if (prev) {
03273 av_image_copy(h->short_ref[0]->f.data, h->short_ref[0]->f.linesize,
03274 (const uint8_t **)prev->f.data, prev->f.linesize,
03275 s->avctx->pix_fmt, s->mb_width * 16, s->mb_height * 16);
03276 h->short_ref[0]->poc = prev->poc + 2;
03277 }
03278 h->short_ref[0]->frame_num = h->prev_frame_num;
03279 }
03280 }
03281
03282
03283
03284
03285 if (s0->first_field) {
03286 assert(s0->current_picture_ptr);
03287 assert(s0->current_picture_ptr->f.data[0]);
03288 assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
03289
03290
03291 if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
03292
03293
03294 s0->current_picture_ptr = NULL;
03295 s0->first_field = FIELD_PICTURE;
03296 } else {
03297 if (s0->current_picture_ptr->frame_num != h->frame_num) {
03298 ff_thread_report_progress((AVFrame*)s0->current_picture_ptr, INT_MAX,
03299 s0->picture_structure==PICT_BOTTOM_FIELD);
03300
03301
03302
03303 s0->first_field = 1;
03304 s0->current_picture_ptr = NULL;
03305 } else {
03306
03307 s0->first_field = 0;
03308 }
03309 }
03310 } else {
03311
03312 assert(!s0->current_picture_ptr);
03313 s0->first_field = FIELD_PICTURE;
03314 }
03315
03316 if (!FIELD_PICTURE || s0->first_field) {
03317 if (ff_h264_frame_start(h) < 0) {
03318 s0->first_field = 0;
03319 return -1;
03320 }
03321 } else {
03322 ff_release_unused_pictures(s, 0);
03323 }
03324 }
03325 if (h != h0)
03326 clone_slice(h, h0);
03327
03328 s->current_picture_ptr->frame_num = h->frame_num;
03329
03330 assert(s->mb_num == s->mb_width * s->mb_height);
03331 if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
03332 first_mb_in_slice >= s->mb_num) {
03333 av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
03334 return -1;
03335 }
03336 s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
03337 s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
03338 if (s->picture_structure == PICT_BOTTOM_FIELD)
03339 s->resync_mb_y = s->mb_y = s->mb_y + 1;
03340 assert(s->mb_y < s->mb_height);
03341
03342 if (s->picture_structure == PICT_FRAME) {
03343 h->curr_pic_num = h->frame_num;
03344 h->max_pic_num = 1 << h->sps.log2_max_frame_num;
03345 } else {
03346 h->curr_pic_num = 2 * h->frame_num + 1;
03347 h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
03348 }
03349
03350 if (h->nal_unit_type == NAL_IDR_SLICE)
03351 get_ue_golomb(&s->gb);
03352
03353 if (h->sps.poc_type == 0) {
03354 h->poc_lsb = get_bits(&s->gb, h->sps.log2_max_poc_lsb);
03355
03356 if (h->pps.pic_order_present == 1 && s->picture_structure == PICT_FRAME)
03357 h->delta_poc_bottom = get_se_golomb(&s->gb);
03358 }
03359
03360 if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
03361 h->delta_poc[0] = get_se_golomb(&s->gb);
03362
03363 if (h->pps.pic_order_present == 1 && s->picture_structure == PICT_FRAME)
03364 h->delta_poc[1] = get_se_golomb(&s->gb);
03365 }
03366
03367 init_poc(h);
03368
03369 if (h->pps.redundant_pic_cnt_present)
03370 h->redundant_pic_count = get_ue_golomb(&s->gb);
03371
03372
03373 h->ref_count[0] = h->pps.ref_count[0];
03374 h->ref_count[1] = h->pps.ref_count[1];
03375
03376 if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
03377 unsigned max = s->picture_structure == PICT_FRAME ? 15 : 31;
03378
03379 if (h->slice_type_nos == AV_PICTURE_TYPE_B)
03380 h->direct_spatial_mv_pred = get_bits1(&s->gb);
03381 num_ref_idx_active_override_flag = get_bits1(&s->gb);
03382
03383 if (num_ref_idx_active_override_flag) {
03384 h->ref_count[0] = get_ue_golomb(&s->gb) + 1;
03385 if (h->slice_type_nos == AV_PICTURE_TYPE_B)
03386 h->ref_count[1] = get_ue_golomb(&s->gb) + 1;
03387 }
03388
03389 if (h->ref_count[0]-1 > max || h->ref_count[1]-1 > max){
03390 av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
03391 h->ref_count[0] = h->ref_count[1] = 1;
03392 return AVERROR_INVALIDDATA;
03393 }
03394
03395 if (h->slice_type_nos == AV_PICTURE_TYPE_B)
03396 h->list_count = 2;
03397 else
03398 h->list_count = 1;
03399 } else
03400 h->ref_count[1]= h->ref_count[0]= h->list_count= 0;
03401
03402 if (!default_ref_list_done)
03403 ff_h264_fill_default_ref_list(h);
03404
03405 if (h->slice_type_nos != AV_PICTURE_TYPE_I &&
03406 ff_h264_decode_ref_pic_list_reordering(h) < 0) {
03407 h->ref_count[1] = h->ref_count[0] = 0;
03408 return -1;
03409 }
03410
03411 if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
03412 s->last_picture_ptr = &h->ref_list[0][0];
03413 ff_copy_picture(&s->last_picture, s->last_picture_ptr);
03414 }
03415 if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
03416 s->next_picture_ptr = &h->ref_list[1][0];
03417 ff_copy_picture(&s->next_picture, s->next_picture_ptr);
03418 }
03419
03420 if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
03421 (h->pps.weighted_bipred_idc == 1 &&
03422 h->slice_type_nos == AV_PICTURE_TYPE_B))
03423 pred_weight_table(h);
03424 else if (h->pps.weighted_bipred_idc == 2 &&
03425 h->slice_type_nos == AV_PICTURE_TYPE_B) {
03426 implicit_weight_table(h, -1);
03427 } else {
03428 h->use_weight = 0;
03429 for (i = 0; i < 2; i++) {
03430 h->luma_weight_flag[i] = 0;
03431 h->chroma_weight_flag[i] = 0;
03432 }
03433 }
03434
03435 if (h->nal_ref_idc && ff_h264_decode_ref_pic_marking(h0, &s->gb) < 0 &&
03436 (s->avctx->err_recognition & AV_EF_EXPLODE))
03437 return AVERROR_INVALIDDATA;
03438
03439 if (FRAME_MBAFF) {
03440 ff_h264_fill_mbaff_ref_list(h);
03441
03442 if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
03443 implicit_weight_table(h, 0);
03444 implicit_weight_table(h, 1);
03445 }
03446 }
03447
03448 if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
03449 ff_h264_direct_dist_scale_factor(h);
03450 ff_h264_direct_ref_list_init(h);
03451
03452 if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
03453 tmp = get_ue_golomb_31(&s->gb);
03454 if (tmp > 2) {
03455 av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
03456 return -1;
03457 }
03458 h->cabac_init_idc = tmp;
03459 }
03460
03461 h->last_qscale_diff = 0;
03462 tmp = h->pps.init_qp + get_se_golomb(&s->gb);
03463 if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
03464 av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
03465 return -1;
03466 }
03467 s->qscale = tmp;
03468 h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
03469 h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
03470
03471 if (h->slice_type == AV_PICTURE_TYPE_SP)
03472 get_bits1(&s->gb);
03473 if (h->slice_type == AV_PICTURE_TYPE_SP ||
03474 h->slice_type == AV_PICTURE_TYPE_SI)
03475 get_se_golomb(&s->gb);
03476
03477 h->deblocking_filter = 1;
03478 h->slice_alpha_c0_offset = 52;
03479 h->slice_beta_offset = 52;
03480 if (h->pps.deblocking_filter_parameters_present) {
03481 tmp = get_ue_golomb_31(&s->gb);
03482 if (tmp > 2) {
03483 av_log(s->avctx, AV_LOG_ERROR,
03484 "deblocking_filter_idc %u out of range\n", tmp);
03485 return -1;
03486 }
03487 h->deblocking_filter = tmp;
03488 if (h->deblocking_filter < 2)
03489 h->deblocking_filter ^= 1;
03490
03491 if (h->deblocking_filter) {
03492 h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
03493 h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
03494 if (h->slice_alpha_c0_offset > 104U ||
03495 h->slice_beta_offset > 104U) {
03496 av_log(s->avctx, AV_LOG_ERROR,
03497 "deblocking filter parameters %d %d out of range\n",
03498 h->slice_alpha_c0_offset, h->slice_beta_offset);
03499 return -1;
03500 }
03501 }
03502 }
03503
03504 if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
03505 (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
03506 h->slice_type_nos != AV_PICTURE_TYPE_I) ||
03507 (s->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
03508 h->slice_type_nos == AV_PICTURE_TYPE_B) ||
03509 (s->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
03510 h->nal_ref_idc == 0))
03511 h->deblocking_filter = 0;
03512
03513 if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
03514 if (s->avctx->flags2 & CODEC_FLAG2_FAST) {
03515
03516
03517 h->deblocking_filter = 2;
03518 } else {
03519 h0->max_contexts = 1;
03520 if (!h0->single_decode_warning) {
03521 av_log(s->avctx, AV_LOG_INFO,
03522 "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
03523 h0->single_decode_warning = 1;
03524 }
03525 if (h != h0) {
03526 av_log(h->s.avctx, AV_LOG_ERROR,
03527 "Deblocking switched inside frame.\n");
03528 return 1;
03529 }
03530 }
03531 }
03532 h->qp_thresh = 15 + 52 -
03533 FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
03534 FFMAX3(0,
03535 h->pps.chroma_qp_index_offset[0],
03536 h->pps.chroma_qp_index_offset[1]) +
03537 6 * (h->sps.bit_depth_luma - 8);
03538
03539 h0->last_slice_type = slice_type;
03540 h->slice_num = ++h0->current_slice;
03541
03542 if (h->slice_num)
03543 h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= s->resync_mb_y;
03544 if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= s->resync_mb_y
03545 && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= s->resync_mb_y
03546 && h->slice_num >= MAX_SLICES) {
03547
03548 av_log(s->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);
03549 }
03550
03551 for (j = 0; j < 2; j++) {
03552 int id_list[16];
03553 int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
03554 for (i = 0; i < 16; i++) {
03555 id_list[i] = 60;
03556 if (h->ref_list[j][i].f.data[0]) {
03557 int k;
03558 uint8_t *base = h->ref_list[j][i].f.base[0];
03559 for (k = 0; k < h->short_ref_count; k++)
03560 if (h->short_ref[k]->f.base[0] == base) {
03561 id_list[i] = k;
03562 break;
03563 }
03564 for (k = 0; k < h->long_ref_count; k++)
03565 if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
03566 id_list[i] = h->short_ref_count + k;
03567 break;
03568 }
03569 }
03570 }
03571
03572 ref2frm[0] =
03573 ref2frm[1] = -1;
03574 for (i = 0; i < 16; i++)
03575 ref2frm[i + 2] = 4 * id_list[i] +
03576 (h->ref_list[j][i].f.reference & 3);
03577 ref2frm[18 + 0] =
03578 ref2frm[18 + 1] = -1;
03579 for (i = 16; i < 48; i++)
03580 ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
03581 (h->ref_list[j][i].f.reference & 3);
03582 }
03583
03584
03585 h->emu_edge_width = (s->flags & CODEC_FLAG_EMU_EDGE ||
03586 (!h->sps.frame_mbs_only_flag &&
03587 s->avctx->active_thread_type))
03588 ? 0 : 16;
03589 h->emu_edge_height = (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
03590
03591 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
03592 av_log(h->s.avctx, AV_LOG_DEBUG,
03593 "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",
03594 h->slice_num,
03595 (s->picture_structure == PICT_FRAME ? "F" : s->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
03596 first_mb_in_slice,
03597 av_get_picture_type_char(h->slice_type),
03598 h->slice_type_fixed ? " fix" : "",
03599 h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
03600 pps_id, h->frame_num,
03601 s->current_picture_ptr->field_poc[0],
03602 s->current_picture_ptr->field_poc[1],
03603 h->ref_count[0], h->ref_count[1],
03604 s->qscale,
03605 h->deblocking_filter,
03606 h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,
03607 h->use_weight,
03608 h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
03609 h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
03610 }
03611
03612 return 0;
03613 }
03614
03615 int ff_h264_get_slice_type(const H264Context *h)
03616 {
03617 switch (h->slice_type) {
03618 case AV_PICTURE_TYPE_P:
03619 return 0;
03620 case AV_PICTURE_TYPE_B:
03621 return 1;
03622 case AV_PICTURE_TYPE_I:
03623 return 2;
03624 case AV_PICTURE_TYPE_SP:
03625 return 3;
03626 case AV_PICTURE_TYPE_SI:
03627 return 4;
03628 default:
03629 return -1;
03630 }
03631 }
03632
03633 static av_always_inline void fill_filter_caches_inter(H264Context *h,
03634 MpegEncContext *const s,
03635 int mb_type, int top_xy,
03636 int left_xy[LEFT_MBS],
03637 int top_type,
03638 int left_type[LEFT_MBS],
03639 int mb_xy, int list)
03640 {
03641 int b_stride = h->b_stride;
03642 int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
03643 int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
03644 if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
03645 if (USES_LIST(top_type, list)) {
03646 const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
03647 const int b8_xy = 4 * top_xy + 2;
03648 int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
03649 AV_COPY128(mv_dst - 1 * 8, s->current_picture.f.motion_val[list][b_xy + 0]);
03650 ref_cache[0 - 1 * 8] =
03651 ref_cache[1 - 1 * 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 0]];
03652 ref_cache[2 - 1 * 8] =
03653 ref_cache[3 - 1 * 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 1]];
03654 } else {
03655 AV_ZERO128(mv_dst - 1 * 8);
03656 AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
03657 }
03658
03659 if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
03660 if (USES_LIST(left_type[LTOP], list)) {
03661 const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
03662 const int b8_xy = 4 * left_xy[LTOP] + 1;
03663 int (*ref2frm)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
03664 AV_COPY32(mv_dst - 1 + 0, s->current_picture.f.motion_val[list][b_xy + b_stride * 0]);
03665 AV_COPY32(mv_dst - 1 + 8, s->current_picture.f.motion_val[list][b_xy + b_stride * 1]);
03666 AV_COPY32(mv_dst - 1 + 16, s->current_picture.f.motion_val[list][b_xy + b_stride * 2]);
03667 AV_COPY32(mv_dst - 1 + 24, s->current_picture.f.motion_val[list][b_xy + b_stride * 3]);
03668 ref_cache[-1 + 0] =
03669 ref_cache[-1 + 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2 * 0]];
03670 ref_cache[-1 + 16] =
03671 ref_cache[-1 + 24] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2 * 1]];
03672 } else {
03673 AV_ZERO32(mv_dst - 1 + 0);
03674 AV_ZERO32(mv_dst - 1 + 8);
03675 AV_ZERO32(mv_dst - 1 + 16);
03676 AV_ZERO32(mv_dst - 1 + 24);
03677 ref_cache[-1 + 0] =
03678 ref_cache[-1 + 8] =
03679 ref_cache[-1 + 16] =
03680 ref_cache[-1 + 24] = LIST_NOT_USED;
03681 }
03682 }
03683 }
03684
03685 if (!USES_LIST(mb_type, list)) {
03686 fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
03687 AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
03688 AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
03689 AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
03690 AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
03691 return;
03692 }
03693
03694 {
03695 int8_t *ref = &s->current_picture.f.ref_index[list][4 * mb_xy];
03696 int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
03697 uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
03698 uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
03699 AV_WN32A(&ref_cache[0 * 8], ref01);
03700 AV_WN32A(&ref_cache[1 * 8], ref01);
03701 AV_WN32A(&ref_cache[2 * 8], ref23);
03702 AV_WN32A(&ref_cache[3 * 8], ref23);
03703 }
03704
03705 {
03706 int16_t(*mv_src)[2] = &s->current_picture.f.motion_val[list][4 * s->mb_x + 4 * s->mb_y * b_stride];
03707 AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
03708 AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
03709 AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
03710 AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
03711 }
03712 }
03713
03718 static int fill_filter_caches(H264Context *h, int mb_type)
03719 {
03720 MpegEncContext *const s = &h->s;
03721 const int mb_xy = h->mb_xy;
03722 int top_xy, left_xy[LEFT_MBS];
03723 int top_type, left_type[LEFT_MBS];
03724 uint8_t *nnz;
03725 uint8_t *nnz_cache;
03726
03727 top_xy = mb_xy - (s->mb_stride << MB_FIELD);
03728
03729
03730
03731
03732 left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
03733 if (FRAME_MBAFF) {
03734 const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
03735 const int curr_mb_field_flag = IS_INTERLACED(mb_type);
03736 if (s->mb_y & 1) {
03737 if (left_mb_field_flag != curr_mb_field_flag)
03738 left_xy[LTOP] -= s->mb_stride;
03739 } else {
03740 if (curr_mb_field_flag)
03741 top_xy += s->mb_stride &
03742 (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
03743 if (left_mb_field_flag != curr_mb_field_flag)
03744 left_xy[LBOT] += s->mb_stride;
03745 }
03746 }
03747
03748 h->top_mb_xy = top_xy;
03749 h->left_mb_xy[LTOP] = left_xy[LTOP];
03750 h->left_mb_xy[LBOT] = left_xy[LBOT];
03751 {
03752
03753
03754
03755 int qp_thresh = h->qp_thresh;
03756 int qp = s->current_picture.f.qscale_table[mb_xy];
03757 if (qp <= qp_thresh &&
03758 (left_xy[LTOP] < 0 ||
03759 ((qp + s->current_picture.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
03760 (top_xy < 0 ||
03761 ((qp + s->current_picture.f.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
03762 if (!FRAME_MBAFF)
03763 return 1;
03764 if ((left_xy[LTOP] < 0 ||
03765 ((qp + s->current_picture.f.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
03766 (top_xy < s->mb_stride ||
03767 ((qp + s->current_picture.f.qscale_table[top_xy - s->mb_stride] + 1) >> 1) <= qp_thresh))
03768 return 1;
03769 }
03770 }
03771
03772 top_type = s->current_picture.f.mb_type[top_xy];
03773 left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
03774 left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
03775 if (h->deblocking_filter == 2) {
03776 if (h->slice_table[top_xy] != h->slice_num)
03777 top_type = 0;
03778 if (h->slice_table[left_xy[LBOT]] != h->slice_num)
03779 left_type[LTOP] = left_type[LBOT] = 0;
03780 } else {
03781 if (h->slice_table[top_xy] == 0xFFFF)
03782 top_type = 0;
03783 if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
03784 left_type[LTOP] = left_type[LBOT] = 0;
03785 }
03786 h->top_type = top_type;
03787 h->left_type[LTOP] = left_type[LTOP];
03788 h->left_type[LBOT] = left_type[LBOT];
03789
03790 if (IS_INTRA(mb_type))
03791 return 0;
03792
03793 fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy,
03794 top_type, left_type, mb_xy, 0);
03795 if (h->list_count == 2)
03796 fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy,
03797 top_type, left_type, mb_xy, 1);
03798
03799 nnz = h->non_zero_count[mb_xy];
03800 nnz_cache = h->non_zero_count_cache;
03801 AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
03802 AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
03803 AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
03804 AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
03805 h->cbp = h->cbp_table[mb_xy];
03806
03807 if (top_type) {
03808 nnz = h->non_zero_count[top_xy];
03809 AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
03810 }
03811
03812 if (left_type[LTOP]) {
03813 nnz = h->non_zero_count[left_xy[LTOP]];
03814 nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
03815 nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
03816 nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
03817 nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
03818 }
03819
03820
03821
03822 if (!CABAC && h->pps.transform_8x8_mode) {
03823 if (IS_8x8DCT(top_type)) {
03824 nnz_cache[4 + 8 * 0] =
03825 nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
03826 nnz_cache[6 + 8 * 0] =
03827 nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
03828 }
03829 if (IS_8x8DCT(left_type[LTOP])) {
03830 nnz_cache[3 + 8 * 1] =
03831 nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12;
03832 }
03833 if (IS_8x8DCT(left_type[LBOT])) {
03834 nnz_cache[3 + 8 * 3] =
03835 nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12;
03836 }
03837
03838 if (IS_8x8DCT(mb_type)) {
03839 nnz_cache[scan8[0]] =
03840 nnz_cache[scan8[1]] =
03841 nnz_cache[scan8[2]] =
03842 nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
03843
03844 nnz_cache[scan8[0 + 4]] =
03845 nnz_cache[scan8[1 + 4]] =
03846 nnz_cache[scan8[2 + 4]] =
03847 nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
03848
03849 nnz_cache[scan8[0 + 8]] =
03850 nnz_cache[scan8[1 + 8]] =
03851 nnz_cache[scan8[2 + 8]] =
03852 nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
03853
03854 nnz_cache[scan8[0 + 12]] =
03855 nnz_cache[scan8[1 + 12]] =
03856 nnz_cache[scan8[2 + 12]] =
03857 nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
03858 }
03859 }
03860
03861 return 0;
03862 }
03863
03864 static void loop_filter(H264Context *h, int start_x, int end_x)
03865 {
03866 MpegEncContext *const s = &h->s;
03867 uint8_t *dest_y, *dest_cb, *dest_cr;
03868 int linesize, uvlinesize, mb_x, mb_y;
03869 const int end_mb_y = s->mb_y + FRAME_MBAFF;
03870 const int old_slice_type = h->slice_type;
03871 const int pixel_shift = h->pixel_shift;
03872 const int block_h = 16 >> s->chroma_y_shift;
03873
03874 if (h->deblocking_filter) {
03875 for (mb_x = start_x; mb_x < end_x; mb_x++)
03876 for (mb_y = end_mb_y - FRAME_MBAFF; mb_y <= end_mb_y; mb_y++) {
03877 int mb_xy, mb_type;
03878 mb_xy = h->mb_xy = mb_x + mb_y * s->mb_stride;
03879 h->slice_num = h->slice_table[mb_xy];
03880 mb_type = s->current_picture.f.mb_type[mb_xy];
03881 h->list_count = h->list_counts[mb_xy];
03882
03883 if (FRAME_MBAFF)
03884 h->mb_mbaff =
03885 h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
03886
03887 s->mb_x = mb_x;
03888 s->mb_y = mb_y;
03889 dest_y = s->current_picture.f.data[0] +
03890 ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
03891 dest_cb = s->current_picture.f.data[1] +
03892 (mb_x << pixel_shift) * (8 << CHROMA444) +
03893 mb_y * s->uvlinesize * block_h;
03894 dest_cr = s->current_picture.f.data[2] +
03895 (mb_x << pixel_shift) * (8 << CHROMA444) +
03896 mb_y * s->uvlinesize * block_h;
03897
03898
03899 if (MB_FIELD) {
03900 linesize = h->mb_linesize = s->linesize * 2;
03901 uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
03902 if (mb_y & 1) {
03903 dest_y -= s->linesize * 15;
03904 dest_cb -= s->uvlinesize * (block_h - 1);
03905 dest_cr -= s->uvlinesize * (block_h - 1);
03906 }
03907 } else {
03908 linesize = h->mb_linesize = s->linesize;
03909 uvlinesize = h->mb_uvlinesize = s->uvlinesize;
03910 }
03911 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
03912 uvlinesize, 0);
03913 if (fill_filter_caches(h, mb_type))
03914 continue;
03915 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.f.qscale_table[mb_xy]);
03916 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.f.qscale_table[mb_xy]);
03917
03918 if (FRAME_MBAFF) {
03919 ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
03920 linesize, uvlinesize);
03921 } else {
03922 ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
03923 dest_cr, linesize, uvlinesize);
03924 }
03925 }
03926 }
03927 h->slice_type = old_slice_type;
03928 s->mb_x = end_x;
03929 s->mb_y = end_mb_y - FRAME_MBAFF;
03930 h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
03931 h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
03932 }
03933
03934 static void predict_field_decoding_flag(H264Context *h)
03935 {
03936 MpegEncContext *const s = &h->s;
03937 const int mb_xy = s->mb_x + s->mb_y * s->mb_stride;
03938 int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
03939 s->current_picture.f.mb_type[mb_xy - 1] :
03940 (h->slice_table[mb_xy - s->mb_stride] == h->slice_num) ?
03941 s->current_picture.f.mb_type[mb_xy - s->mb_stride] : 0;
03942 h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
03943 }
03944
03948 static void decode_finish_row(H264Context *h)
03949 {
03950 MpegEncContext *const s = &h->s;
03951 int top = 16 * (s->mb_y >> FIELD_PICTURE);
03952 int pic_height = 16 * s->mb_height >> FIELD_PICTURE;
03953 int height = 16 << FRAME_MBAFF;
03954 int deblock_border = (16 + 4) << FRAME_MBAFF;
03955
03956 if (h->deblocking_filter) {
03957 if ((top + height) >= pic_height)
03958 height += deblock_border;
03959 top -= deblock_border;
03960 }
03961
03962 if (top >= pic_height || (top + height) < h->emu_edge_height)
03963 return;
03964
03965 height = FFMIN(height, pic_height - top);
03966 if (top < h->emu_edge_height) {
03967 height = top + height;
03968 top = 0;
03969 }
03970
03971 ff_draw_horiz_band(s, top, height);
03972
03973 if (s->dropable)
03974 return;
03975
03976 ff_thread_report_progress(&s->current_picture_ptr->f, top + height - 1,
03977 s->picture_structure == PICT_BOTTOM_FIELD);
03978 }
03979
03980 static int decode_slice(struct AVCodecContext *avctx, void *arg)
03981 {
03982 H264Context *h = *(void **)arg;
03983 MpegEncContext *const s = &h->s;
03984 const int part_mask = s->partitioned_frame ? (ER_AC_END | ER_AC_ERROR)
03985 : 0x7F;
03986 int lf_x_start = s->mb_x;
03987
03988 s->mb_skip_run = -1;
03989
03990 h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME ||
03991 s->codec_id != CODEC_ID_H264 ||
03992 (CONFIG_GRAY && (s->flags & CODEC_FLAG_GRAY));
03993
03994 if (h->pps.cabac) {
03995
03996 align_get_bits(&s->gb);
03997
03998
03999 ff_init_cabac_states(&h->cabac);
04000 ff_init_cabac_decoder(&h->cabac,
04001 s->gb.buffer + get_bits_count(&s->gb) / 8,
04002 (get_bits_left(&s->gb) + 7) / 8);
04003
04004 ff_h264_init_cabac_states(h);
04005
04006 for (;;) {
04007
04008 int ret = ff_h264_decode_mb_cabac(h);
04009 int eos;
04010
04011
04012 if (ret >= 0)
04013 ff_h264_hl_decode_mb(h);
04014
04015
04016 if (ret >= 0 && FRAME_MBAFF) {
04017 s->mb_y++;
04018
04019 ret = ff_h264_decode_mb_cabac(h);
04020
04021 if (ret >= 0)
04022 ff_h264_hl_decode_mb(h);
04023 s->mb_y--;
04024 }
04025 eos = get_cabac_terminate(&h->cabac);
04026
04027 if ((s->workaround_bugs & FF_BUG_TRUNCATED) &&
04028 h->cabac.bytestream > h->cabac.bytestream_end + 2) {
04029 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x - 1,
04030 s->mb_y, ER_MB_END & part_mask);
04031 if (s->mb_x >= lf_x_start)
04032 loop_filter(h, lf_x_start, s->mb_x + 1);
04033 return 0;
04034 }
04035 if (h->cabac.bytestream > h->cabac.bytestream_end + 2 )
04036 av_log(h->s.avctx, AV_LOG_DEBUG, "bytestream overread %td\n", h->cabac.bytestream_end - h->cabac.bytestream);
04037 if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 4) {
04038 av_log(h->s.avctx, AV_LOG_ERROR,
04039 "error while decoding MB %d %d, bytestream (%td)\n",
04040 s->mb_x, s->mb_y,
04041 h->cabac.bytestream_end - h->cabac.bytestream);
04042 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x,
04043 s->mb_y, ER_MB_ERROR & part_mask);
04044 return -1;
04045 }
04046
04047 if (++s->mb_x >= s->mb_width) {
04048 loop_filter(h, lf_x_start, s->mb_x);
04049 s->mb_x = lf_x_start = 0;
04050 decode_finish_row(h);
04051 ++s->mb_y;
04052 if (FIELD_OR_MBAFF_PICTURE) {
04053 ++s->mb_y;
04054 if (FRAME_MBAFF && s->mb_y < s->mb_height)
04055 predict_field_decoding_flag(h);
04056 }
04057 }
04058
04059 if (eos || s->mb_y >= s->mb_height) {
04060 tprintf(s->avctx, "slice end %d %d\n",
04061 get_bits_count(&s->gb), s->gb.size_in_bits);
04062 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x - 1,
04063 s->mb_y, ER_MB_END & part_mask);
04064 if (s->mb_x > lf_x_start)
04065 loop_filter(h, lf_x_start, s->mb_x);
04066 return 0;
04067 }
04068 }
04069 } else {
04070 for (;;) {
04071 int ret = ff_h264_decode_mb_cavlc(h);
04072
04073 if (ret >= 0)
04074 ff_h264_hl_decode_mb(h);
04075
04076
04077 if (ret >= 0 && FRAME_MBAFF) {
04078 s->mb_y++;
04079 ret = ff_h264_decode_mb_cavlc(h);
04080
04081 if (ret >= 0)
04082 ff_h264_hl_decode_mb(h);
04083 s->mb_y--;
04084 }
04085
04086 if (ret < 0) {
04087 av_log(h->s.avctx, AV_LOG_ERROR,
04088 "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
04089 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x,
04090 s->mb_y, ER_MB_ERROR & part_mask);
04091 return -1;
04092 }
04093
04094 if (++s->mb_x >= s->mb_width) {
04095 loop_filter(h, lf_x_start, s->mb_x);
04096 s->mb_x = lf_x_start = 0;
04097 decode_finish_row(h);
04098 ++s->mb_y;
04099 if (FIELD_OR_MBAFF_PICTURE) {
04100 ++s->mb_y;
04101 if (FRAME_MBAFF && s->mb_y < s->mb_height)
04102 predict_field_decoding_flag(h);
04103 }
04104 if (s->mb_y >= s->mb_height) {
04105 tprintf(s->avctx, "slice end %d %d\n",
04106 get_bits_count(&s->gb), s->gb.size_in_bits);
04107
04108 if ( get_bits_left(&s->gb) == 0
04109 || get_bits_left(&s->gb) > 0 && !(s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
04110 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y,
04111 s->mb_x - 1, s->mb_y,
04112 ER_MB_END & part_mask);
04113
04114 return 0;
04115 } else {
04116 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y,
04117 s->mb_x, s->mb_y,
04118 ER_MB_END & part_mask);
04119
04120 return -1;
04121 }
04122 }
04123 }
04124
04125 if (get_bits_left(&s->gb) <= 0 && s->mb_skip_run <= 0) {
04126 tprintf(s->avctx, "slice end %d %d\n",
04127 get_bits_count(&s->gb), s->gb.size_in_bits);
04128 if (get_bits_left(&s->gb) == 0) {
04129 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y,
04130 s->mb_x - 1, s->mb_y,
04131 ER_MB_END & part_mask);
04132 if (s->mb_x > lf_x_start)
04133 loop_filter(h, lf_x_start, s->mb_x);
04134
04135 return 0;
04136 } else {
04137 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x,
04138 s->mb_y, ER_MB_ERROR & part_mask);
04139
04140 return -1;
04141 }
04142 }
04143 }
04144 }
04145 }
04146
04153 static int execute_decode_slices(H264Context *h, int context_count)
04154 {
04155 MpegEncContext *const s = &h->s;
04156 AVCodecContext *const avctx = s->avctx;
04157 H264Context *hx;
04158 int i;
04159
04160 if (s->avctx->hwaccel ||
04161 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
04162 return 0;
04163 if (context_count == 1) {
04164 return decode_slice(avctx, &h);
04165 } else {
04166 for (i = 1; i < context_count; i++) {
04167 hx = h->thread_context[i];
04168 hx->s.err_recognition = avctx->err_recognition;
04169 hx->s.error_count = 0;
04170 hx->x264_build = h->x264_build;
04171 }
04172
04173 avctx->execute(avctx, decode_slice, h->thread_context,
04174 NULL, context_count, sizeof(void *));
04175
04176
04177 hx = h->thread_context[context_count - 1];
04178 s->mb_x = hx->s.mb_x;
04179 s->mb_y = hx->s.mb_y;
04180 s->dropable = hx->s.dropable;
04181 s->picture_structure = hx->s.picture_structure;
04182 for (i = 1; i < context_count; i++)
04183 h->s.error_count += h->thread_context[i]->s.error_count;
04184 }
04185
04186 return 0;
04187 }
04188
04189 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
04190 {
04191 MpegEncContext *const s = &h->s;
04192 AVCodecContext *const avctx = s->avctx;
04193 H264Context *hx;
04194 int buf_index;
04195 int context_count;
04196 int next_avc;
04197 int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
04198 int nals_needed = 0;
04199 int nal_index;
04200
04201 h->nal_unit_type= 0;
04202
04203 if(!s->slice_context_count)
04204 s->slice_context_count= 1;
04205 h->max_contexts = s->slice_context_count;
04206 if (!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
04207 h->current_slice = 0;
04208 if (!s->first_field)
04209 s->current_picture_ptr = NULL;
04210 ff_h264_reset_sei(h);
04211 }
04212
04213 for (; pass <= 1; pass++) {
04214 buf_index = 0;
04215 context_count = 0;
04216 next_avc = h->is_avc ? 0 : buf_size;
04217 nal_index = 0;
04218 for (;;) {
04219 int consumed;
04220 int dst_length;
04221 int bit_length;
04222 const uint8_t *ptr;
04223 int i, nalsize = 0;
04224 int err;
04225
04226 if (buf_index >= next_avc) {
04227 if (buf_index >= buf_size - h->nal_length_size)
04228 break;
04229 nalsize = 0;
04230 for (i = 0; i < h->nal_length_size; i++)
04231 nalsize = (nalsize << 8) | buf[buf_index++];
04232 if (nalsize <= 0 || nalsize > buf_size - buf_index) {
04233 av_log(h->s.avctx, AV_LOG_ERROR,
04234 "AVC: nal size %d\n", nalsize);
04235 break;
04236 }
04237 next_avc = buf_index + nalsize;
04238 } else {
04239
04240 for (; buf_index + 3 < next_avc; buf_index++)
04241
04242 if (buf[buf_index] == 0 &&
04243 buf[buf_index + 1] == 0 &&
04244 buf[buf_index + 2] == 1)
04245 break;
04246
04247 if (buf_index + 3 >= buf_size)
04248 break;
04249
04250 buf_index += 3;
04251 if (buf_index >= next_avc)
04252 continue;
04253 }
04254
04255 hx = h->thread_context[context_count];
04256
04257 ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
04258 &consumed, next_avc - buf_index);
04259 if (ptr == NULL || dst_length < 0) {
04260 buf_index = -1;
04261 goto end;
04262 }
04263 i = buf_index + consumed;
04264 if ((s->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
04265 buf[i] == 0x00 && buf[i + 1] == 0x00 &&
04266 buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
04267 s->workaround_bugs |= FF_BUG_TRUNCATED;
04268
04269 if (!(s->workaround_bugs & FF_BUG_TRUNCATED))
04270 while(dst_length > 0 && ptr[dst_length - 1] == 0)
04271 dst_length--;
04272 bit_length = !dst_length ? 0
04273 : (8 * dst_length -
04274 decode_rbsp_trailing(h, ptr + dst_length - 1));
04275
04276 if (s->avctx->debug & FF_DEBUG_STARTCODE)
04277 av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d/%d at %d/%d length %d pass %d\n", hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length, pass);
04278
04279 if (h->is_avc && (nalsize != consumed) && nalsize)
04280 av_log(h->s.avctx, AV_LOG_DEBUG,
04281 "AVC: Consumed only %d bytes instead of %d\n",
04282 consumed, nalsize);
04283
04284 buf_index += consumed;
04285 nal_index++;
04286
04287 if (pass == 0) {
04288
04289
04290
04291
04292 switch (hx->nal_unit_type) {
04293 case NAL_SPS:
04294 case NAL_PPS:
04295 nals_needed = nal_index;
04296 break;
04297 case NAL_IDR_SLICE:
04298 case NAL_SLICE:
04299 init_get_bits(&hx->s.gb, ptr, bit_length);
04300 if (!get_ue_golomb(&hx->s.gb))
04301 nals_needed = nal_index;
04302 }
04303 continue;
04304 }
04305
04306
04307 if (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
04308 continue;
04309
04310 again:
04311 err = 0;
04312 switch (hx->nal_unit_type) {
04313 case NAL_IDR_SLICE:
04314 if (h->nal_unit_type != NAL_IDR_SLICE) {
04315 av_log(h->s.avctx, AV_LOG_ERROR,
04316 "Invalid mix of idr and non-idr slices\n");
04317 buf_index = -1;
04318 goto end;
04319 }
04320 idr(h);
04321 case NAL_SLICE:
04322 init_get_bits(&hx->s.gb, ptr, bit_length);
04323 hx->intra_gb_ptr =
04324 hx->inter_gb_ptr = &hx->s.gb;
04325 hx->s.data_partitioning = 0;
04326
04327 if ((err = decode_slice_header(hx, h)))
04328 break;
04329
04330 if ( h->sei_recovery_frame_cnt >= 0
04331 && ( h->recovery_frame<0
04332 || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt)) {
04333 h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) %
04334 (1 << h->sps.log2_max_frame_num);
04335 }
04336
04337 s->current_picture_ptr->f.key_frame |=
04338 (hx->nal_unit_type == NAL_IDR_SLICE);
04339
04340 if (h->recovery_frame == h->frame_num) {
04341 s->current_picture_ptr->sync |= 1;
04342 h->recovery_frame = -1;
04343 }
04344
04345 h->sync |= !!s->current_picture_ptr->f.key_frame;
04346 h->sync |= 3*!!(s->flags2 & CODEC_FLAG2_SHOW_ALL);
04347 s->current_picture_ptr->sync |= h->sync;
04348
04349 if (h->current_slice == 1) {
04350 if (!(s->flags2 & CODEC_FLAG2_CHUNKS))
04351 decode_postinit(h, nal_index >= nals_needed);
04352
04353 if (s->avctx->hwaccel &&
04354 s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
04355 return -1;
04356 if (CONFIG_H264_VDPAU_DECODER &&
04357 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
04358 ff_vdpau_h264_picture_start(s);
04359 }
04360
04361 if (hx->redundant_pic_count == 0 &&
04362 (avctx->skip_frame < AVDISCARD_NONREF ||
04363 hx->nal_ref_idc) &&
04364 (avctx->skip_frame < AVDISCARD_BIDIR ||
04365 hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
04366 (avctx->skip_frame < AVDISCARD_NONKEY ||
04367 hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
04368 avctx->skip_frame < AVDISCARD_ALL) {
04369 if (avctx->hwaccel) {
04370 if (avctx->hwaccel->decode_slice(avctx,
04371 &buf[buf_index - consumed],
04372 consumed) < 0)
04373 return -1;
04374 } else if (CONFIG_H264_VDPAU_DECODER &&
04375 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
04376 static const uint8_t start_code[] = {
04377 0x00, 0x00, 0x01 };
04378 ff_vdpau_add_data_chunk(s, start_code,
04379 sizeof(start_code));
04380 ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed],
04381 consumed);
04382 } else
04383 context_count++;
04384 }
04385 break;
04386 case NAL_DPA:
04387 init_get_bits(&hx->s.gb, ptr, bit_length);
04388 hx->intra_gb_ptr =
04389 hx->inter_gb_ptr = NULL;
04390
04391 if ((err = decode_slice_header(hx, h)) < 0)
04392 break;
04393
04394 hx->s.data_partitioning = 1;
04395 break;
04396 case NAL_DPB:
04397 init_get_bits(&hx->intra_gb, ptr, bit_length);
04398 hx->intra_gb_ptr = &hx->intra_gb;
04399 break;
04400 case NAL_DPC:
04401 init_get_bits(&hx->inter_gb, ptr, bit_length);
04402 hx->inter_gb_ptr = &hx->inter_gb;
04403
04404 av_log(h->s.avctx, AV_LOG_ERROR, "Partitioned H.264 support is incomplete\n");
04405 return AVERROR_PATCHWELCOME;
04406
04407 if (hx->redundant_pic_count == 0 &&
04408 hx->intra_gb_ptr &&
04409 hx->s.data_partitioning &&
04410 s->context_initialized &&
04411 (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) &&
04412 (avctx->skip_frame < AVDISCARD_BIDIR ||
04413 hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
04414 (avctx->skip_frame < AVDISCARD_NONKEY ||
04415 hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
04416 avctx->skip_frame < AVDISCARD_ALL)
04417 context_count++;
04418 break;
04419 case NAL_SEI:
04420 init_get_bits(&s->gb, ptr, bit_length);
04421 ff_h264_decode_sei(h);
04422 break;
04423 case NAL_SPS:
04424 init_get_bits(&s->gb, ptr, bit_length);
04425 if (ff_h264_decode_seq_parameter_set(h) < 0 && (h->is_avc ? (nalsize != consumed) && nalsize : 1)) {
04426 av_log(h->s.avctx, AV_LOG_DEBUG,
04427 "SPS decoding failure, trying alternative mode\n");
04428 if (h->is_avc)
04429 av_assert0(next_avc - buf_index + consumed == nalsize);
04430 init_get_bits(&s->gb, &buf[buf_index + 1 - consumed],
04431 8*(next_avc - buf_index + consumed - 1));
04432 ff_h264_decode_seq_parameter_set(h);
04433 }
04434
04435 if (s->flags & CODEC_FLAG_LOW_DELAY ||
04436 (h->sps.bitstream_restriction_flag &&
04437 !h->sps.num_reorder_frames))
04438 s->low_delay = 1;
04439 if (avctx->has_b_frames < 2)
04440 avctx->has_b_frames = !s->low_delay;
04441 break;
04442 case NAL_PPS:
04443 init_get_bits(&s->gb, ptr, bit_length);
04444 ff_h264_decode_picture_parameter_set(h, bit_length);
04445 break;
04446 case NAL_AUD:
04447 case NAL_END_SEQUENCE:
04448 case NAL_END_STREAM:
04449 case NAL_FILLER_DATA:
04450 case NAL_SPS_EXT:
04451 case NAL_AUXILIARY_SLICE:
04452 break;
04453 default:
04454 av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
04455 hx->nal_unit_type, bit_length);
04456 }
04457
04458 if (context_count == h->max_contexts) {
04459 execute_decode_slices(h, context_count);
04460 context_count = 0;
04461 }
04462
04463 if (err < 0)
04464 av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
04465 else if (err == 1) {
04466
04467
04468
04469
04470 h->nal_unit_type = hx->nal_unit_type;
04471 h->nal_ref_idc = hx->nal_ref_idc;
04472 hx = h;
04473 goto again;
04474 }
04475 }
04476 }
04477 if (context_count)
04478 execute_decode_slices(h, context_count);
04479
04480 end:
04481
04482 if (s->current_picture_ptr && s->current_picture_ptr->owner2 == s &&
04483 !s->dropable) {
04484 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
04485 s->picture_structure == PICT_BOTTOM_FIELD);
04486 }
04487
04488 return buf_index;
04489 }
04490
04494 static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size)
04495 {
04496 if (pos == 0)
04497 pos = 1;
04498 if (pos + 10 > buf_size)
04499 pos = buf_size;
04500
04501 return pos;
04502 }
04503
04504 static int decode_frame(AVCodecContext *avctx, void *data,
04505 int *data_size, AVPacket *avpkt)
04506 {
04507 const uint8_t *buf = avpkt->data;
04508 int buf_size = avpkt->size;
04509 H264Context *h = avctx->priv_data;
04510 MpegEncContext *s = &h->s;
04511 AVFrame *pict = data;
04512 int buf_index = 0;
04513 Picture *out;
04514 int i, out_idx;
04515
04516 s->flags = avctx->flags;
04517 s->flags2 = avctx->flags2;
04518
04519
04520 if (buf_size == 0) {
04521 out:
04522
04523 s->current_picture_ptr = NULL;
04524
04525
04526 out = h->delayed_pic[0];
04527 out_idx = 0;
04528 for (i = 1;
04529 h->delayed_pic[i] &&
04530 !h->delayed_pic[i]->f.key_frame &&
04531 !h->delayed_pic[i]->mmco_reset;
04532 i++)
04533 if (h->delayed_pic[i]->poc < out->poc) {
04534 out = h->delayed_pic[i];
04535 out_idx = i;
04536 }
04537
04538 for (i = out_idx; h->delayed_pic[i]; i++)
04539 h->delayed_pic[i] = h->delayed_pic[i + 1];
04540
04541 if (out) {
04542 *data_size = sizeof(AVFrame);
04543 *pict = out->f;
04544 }
04545
04546 return buf_index;
04547 }
04548 if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
04549 int cnt= buf[5]&0x1f;
04550 const uint8_t *p= buf+6;
04551 while(cnt--){
04552 int nalsize= AV_RB16(p) + 2;
04553 if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
04554 goto not_extra;
04555 p += nalsize;
04556 }
04557 cnt = *(p++);
04558 if(!cnt)
04559 goto not_extra;
04560 while(cnt--){
04561 int nalsize= AV_RB16(p) + 2;
04562 if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
04563 goto not_extra;
04564 p += nalsize;
04565 }
04566
04567 return ff_h264_decode_extradata(h, buf, buf_size);
04568 }
04569 not_extra:
04570
04571 buf_index = decode_nal_units(h, buf, buf_size);
04572 if (buf_index < 0)
04573 return -1;
04574
04575 if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
04576 av_assert0(buf_index <= buf_size);
04577 goto out;
04578 }
04579
04580 if (!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr) {
04581 if (avctx->skip_frame >= AVDISCARD_NONREF ||
04582 buf_size >= 4 && !memcmp("Q264", buf, 4))
04583 return buf_size;
04584 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
04585 return -1;
04586 }
04587
04588 if (!(s->flags2 & CODEC_FLAG2_CHUNKS) ||
04589 (s->mb_y >= s->mb_height && s->mb_height)) {
04590 if (s->flags2 & CODEC_FLAG2_CHUNKS)
04591 decode_postinit(h, 1);
04592
04593 field_end(h, 0);
04594
04595
04596 *data_size = 0;
04597 if (h->next_output_pic && (h->next_output_pic->sync || h->sync>1)) {
04598 *data_size = sizeof(AVFrame);
04599 *pict = h->next_output_pic->f;
04600 }
04601 }
04602
04603 assert(pict->data[0] || !*data_size);
04604 ff_print_debug_info(s, pict);
04605
04606
04607 return get_consumed_bytes(s, buf_index, buf_size);
04608 }
04609
04610 av_cold void ff_h264_free_context(H264Context *h)
04611 {
04612 int i;
04613
04614 free_tables(h, 1);
04615
04616 for (i = 0; i < MAX_SPS_COUNT; i++)
04617 av_freep(h->sps_buffers + i);
04618
04619 for (i = 0; i < MAX_PPS_COUNT; i++)
04620 av_freep(h->pps_buffers + i);
04621 }
04622
04623 static av_cold int h264_decode_end(AVCodecContext *avctx)
04624 {
04625 H264Context *h = avctx->priv_data;
04626 MpegEncContext *s = &h->s;
04627
04628 ff_h264_remove_all_refs(h);
04629 ff_h264_free_context(h);
04630
04631 ff_MPV_common_end(s);
04632
04633
04634
04635 return 0;
04636 }
04637
04638 static const AVProfile profiles[] = {
04639 { FF_PROFILE_H264_BASELINE, "Baseline" },
04640 { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
04641 { FF_PROFILE_H264_MAIN, "Main" },
04642 { FF_PROFILE_H264_EXTENDED, "Extended" },
04643 { FF_PROFILE_H264_HIGH, "High" },
04644 { FF_PROFILE_H264_HIGH_10, "High 10" },
04645 { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
04646 { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
04647 { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
04648 { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
04649 { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
04650 { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
04651 { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
04652 { FF_PROFILE_UNKNOWN },
04653 };
04654
04655 static const AVOption h264_options[] = {
04656 {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, 0},
04657 {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 4, 0},
04658 {NULL}
04659 };
04660
04661 static const AVClass h264_class = {
04662 "H264 Decoder",
04663 av_default_item_name,
04664 h264_options,
04665 LIBAVUTIL_VERSION_INT,
04666 };
04667
04668 static const AVClass h264_vdpau_class = {
04669 "H264 VDPAU Decoder",
04670 av_default_item_name,
04671 h264_options,
04672 LIBAVUTIL_VERSION_INT,
04673 };
04674
04675 AVCodec ff_h264_decoder = {
04676 .name = "h264",
04677 .type = AVMEDIA_TYPE_VIDEO,
04678 .id = CODEC_ID_H264,
04679 .priv_data_size = sizeof(H264Context),
04680 .init = ff_h264_decode_init,
04681 .close = h264_decode_end,
04682 .decode = decode_frame,
04683 .capabilities = CODEC_CAP_DR1 |
04684 CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
04685 CODEC_CAP_FRAME_THREADS,
04686 .flush = flush_dpb,
04687 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
04688 .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
04689 .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
04690 .profiles = NULL_IF_CONFIG_SMALL(profiles),
04691 .priv_class = &h264_class,
04692 };
04693
04694 #if CONFIG_H264_VDPAU_DECODER
04695 AVCodec ff_h264_vdpau_decoder = {
04696 .name = "h264_vdpau",
04697 .type = AVMEDIA_TYPE_VIDEO,
04698 .id = CODEC_ID_H264,
04699 .priv_data_size = sizeof(H264Context),
04700 .init = ff_h264_decode_init,
04701 .close = h264_decode_end,
04702 .decode = decode_frame,
04703 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
04704 .flush = flush_dpb,
04705 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
04706 .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_VDPAU_H264,
04707 PIX_FMT_NONE},
04708 .profiles = NULL_IF_CONFIG_SMALL(profiles),
04709 .priv_class = &h264_vdpau_class,
04710 };
04711 #endif