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