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