2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #include "libavcore/imgutils.h"
32 #include "mpegvideo.h"
35 #include "h264_mvpred.h"
36 #include "h264_parser.h"
39 #include "rectangle.h"
40 #include "vdpau_internal.h"
41 #include "libavutil/avassert.h"
48 static const uint8_t rem6[52]={
49 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
52 static const uint8_t div6[52]={
53 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
56 static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
63 void ff_h264_write_back_intra_pred_mode(H264Context *h){
64 int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
66 AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
67 mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
68 mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
69 mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
73 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
75 int ff_h264_check_intra4x4_pred_mode(H264Context *h){
76 MpegEncContext * const s = &h->s;
77 static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
78 static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
81 if(!(h->top_samples_available&0x8000)){
83 int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
85 av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
88 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
93 if((h->left_samples_available&0x8888)!=0x8888){
94 static const int mask[4]={0x8000,0x2000,0x80,0x20};
96 if(!(h->left_samples_available&mask[i])){
97 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
99 av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
102 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
109 } //FIXME cleanup like ff_h264_check_intra_pred_mode
112 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
114 int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
115 MpegEncContext * const s = &h->s;
116 static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
117 static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
120 av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
124 if(!(h->top_samples_available&0x8000)){
127 av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
132 if((h->left_samples_available&0x8080) != 0x8080){
134 if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
135 mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
138 av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
146 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
151 // src[0]&0x80; //forbidden bit
152 h->nal_ref_idc= src[0]>>5;
153 h->nal_unit_type= src[0]&0x1F;
157 for(i=0; i<length; i++)
158 printf("%2X ", src[i]);
161 #if HAVE_FAST_UNALIGNED
164 for(i=0; i+1<length; i+=9){
165 if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
168 for(i=0; i+1<length; i+=5){
169 if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
172 if(i>0 && !src[i]) i--;
176 for(i=0; i+1<length; i+=2){
178 if(i>0 && src[i-1]==0) i--;
180 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
182 /* startcode, so we must be past the end */
190 if(i>=length-1){ //no escaped 0
192 *consumed= length+1; //+1 for the header
196 bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
197 av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
198 dst= h->rbsp_buffer[bufidx];
204 //printf("decoding esc\n");
208 //remove escapes (very rare 1:2^22)
210 dst[di++]= src[si++];
211 dst[di++]= src[si++];
212 }else if(src[si]==0 && src[si+1]==0){
213 if(src[si+2]==3){ //escape
218 }else //next start code
222 dst[di++]= src[si++];
225 dst[di++]= src[si++];
228 memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
231 *consumed= si + 1;//+1 for the header
232 //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
236 int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
240 tprintf(h->s.avctx, "rbsp trailing %X\n", v);
249 static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
250 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
251 int src_x_offset, int src_y_offset,
252 qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
253 MpegEncContext * const s = &h->s;
254 const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
255 int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
256 const int luma_xy= (mx&3) + ((my&3)<<2);
257 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
258 uint8_t * src_cb, * src_cr;
259 int extra_width= h->emu_edge_width;
260 int extra_height= h->emu_edge_height;
262 const int full_mx= mx>>2;
263 const int full_my= my>>2;
264 const int pic_width = 16*s->mb_width;
265 const int pic_height = 16*s->mb_height >> MB_FIELD;
267 if(mx&7) extra_width -= 3;
268 if(my&7) extra_height -= 3;
270 if( full_mx < 0-extra_width
271 || full_my < 0-extra_height
272 || full_mx + 16/*FIXME*/ > pic_width + extra_width
273 || full_my + 16/*FIXME*/ > pic_height + extra_height){
274 ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
275 src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
279 qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
281 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
284 if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
287 // chroma offset when predicting from a field of opposite parity
288 my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
289 emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
291 src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
292 src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
295 ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
296 src_cb= s->edge_emu_buffer;
298 chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
301 ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
302 src_cr= s->edge_emu_buffer;
304 chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
307 static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
308 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
309 int x_offset, int y_offset,
310 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
311 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
312 int list0, int list1){
313 MpegEncContext * const s = &h->s;
314 qpel_mc_func *qpix_op= qpix_put;
315 h264_chroma_mc_func chroma_op= chroma_put;
317 dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
318 dest_cb += x_offset + y_offset*h->mb_uvlinesize;
319 dest_cr += x_offset + y_offset*h->mb_uvlinesize;
320 x_offset += 8*s->mb_x;
321 y_offset += 8*(s->mb_y >> MB_FIELD);
324 Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
325 mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
326 dest_y, dest_cb, dest_cr, x_offset, y_offset,
330 chroma_op= chroma_avg;
334 Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
335 mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
336 dest_y, dest_cb, dest_cr, x_offset, y_offset,
341 static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
342 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
343 int x_offset, int y_offset,
344 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
345 h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
346 h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
347 int list0, int list1){
348 MpegEncContext * const s = &h->s;
350 dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
351 dest_cb += x_offset + y_offset*h->mb_uvlinesize;
352 dest_cr += x_offset + y_offset*h->mb_uvlinesize;
353 x_offset += 8*s->mb_x;
354 y_offset += 8*(s->mb_y >> MB_FIELD);
357 /* don't optimize for luma-only case, since B-frames usually
358 * use implicit weights => chroma too. */
359 uint8_t *tmp_cb = s->obmc_scratchpad;
360 uint8_t *tmp_cr = s->obmc_scratchpad + 8;
361 uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
362 int refn0 = h->ref_cache[0][ scan8[n] ];
363 int refn1 = h->ref_cache[1][ scan8[n] ];
365 mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
366 dest_y, dest_cb, dest_cr,
367 x_offset, y_offset, qpix_put, chroma_put);
368 mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
369 tmp_y, tmp_cb, tmp_cr,
370 x_offset, y_offset, qpix_put, chroma_put);
372 if(h->use_weight == 2){
373 int weight0 = h->implicit_weight[refn0][refn1][s->mb_y&1];
374 int weight1 = 64 - weight0;
375 luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
376 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
377 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
379 luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
380 h->luma_weight[refn0][0][0] , h->luma_weight[refn1][1][0],
381 h->luma_weight[refn0][0][1] + h->luma_weight[refn1][1][1]);
382 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
383 h->chroma_weight[refn0][0][0][0] , h->chroma_weight[refn1][1][0][0],
384 h->chroma_weight[refn0][0][0][1] + h->chroma_weight[refn1][1][0][1]);
385 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
386 h->chroma_weight[refn0][0][1][0] , h->chroma_weight[refn1][1][1][0],
387 h->chroma_weight[refn0][0][1][1] + h->chroma_weight[refn1][1][1][1]);
390 int list = list1 ? 1 : 0;
391 int refn = h->ref_cache[list][ scan8[n] ];
392 Picture *ref= &h->ref_list[list][refn];
393 mc_dir_part(h, ref, n, square, chroma_height, delta, list,
394 dest_y, dest_cb, dest_cr, x_offset, y_offset,
395 qpix_put, chroma_put);
397 luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
398 h->luma_weight[refn][list][0], h->luma_weight[refn][list][1]);
399 if(h->use_weight_chroma){
400 chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
401 h->chroma_weight[refn][list][0][0], h->chroma_weight[refn][list][0][1]);
402 chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
403 h->chroma_weight[refn][list][1][0], h->chroma_weight[refn][list][1][1]);
408 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
409 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
410 int x_offset, int y_offset,
411 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
412 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
413 h264_weight_func *weight_op, h264_biweight_func *weight_avg,
414 int list0, int list1){
415 if((h->use_weight==2 && list0 && list1
416 && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ][h->s.mb_y&1] != 32))
418 mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
419 x_offset, y_offset, qpix_put, chroma_put,
420 weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
422 mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
423 x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
426 static inline void prefetch_motion(H264Context *h, int list){
427 /* fetch pixels for estimated mv 4 macroblocks ahead
428 * optimized for 64byte cache lines */
429 MpegEncContext * const s = &h->s;
430 const int refn = h->ref_cache[list][scan8[0]];
432 const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
433 const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
434 uint8_t **src= h->ref_list[list][refn].data;
435 int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
436 s->dsp.prefetch(src[0]+off, s->linesize, 4);
437 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
438 s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
442 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
443 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
444 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
445 h264_weight_func *weight_op, h264_biweight_func *weight_avg){
446 MpegEncContext * const s = &h->s;
447 const int mb_xy= h->mb_xy;
448 const int mb_type= s->current_picture.mb_type[mb_xy];
450 assert(IS_INTER(mb_type));
452 prefetch_motion(h, 0);
454 if(IS_16X16(mb_type)){
455 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
456 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
457 weight_op, weight_avg,
458 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
459 }else if(IS_16X8(mb_type)){
460 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
461 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
462 &weight_op[1], &weight_avg[1],
463 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
464 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
465 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
466 &weight_op[1], &weight_avg[1],
467 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
468 }else if(IS_8X16(mb_type)){
469 mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
470 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
471 &weight_op[2], &weight_avg[2],
472 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
473 mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
474 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
475 &weight_op[2], &weight_avg[2],
476 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
480 assert(IS_8X8(mb_type));
483 const int sub_mb_type= h->sub_mb_type[i];
485 int x_offset= (i&1)<<2;
486 int y_offset= (i&2)<<1;
488 if(IS_SUB_8X8(sub_mb_type)){
489 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
490 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
491 &weight_op[3], &weight_avg[3],
492 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
493 }else if(IS_SUB_8X4(sub_mb_type)){
494 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
495 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
496 &weight_op[4], &weight_avg[4],
497 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
498 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
499 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
500 &weight_op[4], &weight_avg[4],
501 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
502 }else if(IS_SUB_4X8(sub_mb_type)){
503 mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
504 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
505 &weight_op[5], &weight_avg[5],
506 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
507 mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
508 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
509 &weight_op[5], &weight_avg[5],
510 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
513 assert(IS_SUB_4X4(sub_mb_type));
515 int sub_x_offset= x_offset + 2*(j&1);
516 int sub_y_offset= y_offset + (j&2);
517 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
518 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
519 &weight_op[6], &weight_avg[6],
520 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
526 prefetch_motion(h, 1);
530 static void free_tables(H264Context *h){
533 av_freep(&h->intra4x4_pred_mode);
534 av_freep(&h->chroma_pred_mode_table);
535 av_freep(&h->cbp_table);
536 av_freep(&h->mvd_table[0]);
537 av_freep(&h->mvd_table[1]);
538 av_freep(&h->direct_table);
539 av_freep(&h->non_zero_count);
540 av_freep(&h->slice_table_base);
541 h->slice_table= NULL;
542 av_freep(&h->list_counts);
544 av_freep(&h->mb2b_xy);
545 av_freep(&h->mb2br_xy);
547 for(i = 0; i < MAX_THREADS; i++) {
548 hx = h->thread_context[i];
550 av_freep(&hx->top_borders[1]);
551 av_freep(&hx->top_borders[0]);
552 av_freep(&hx->s.obmc_scratchpad);
553 av_freep(&hx->rbsp_buffer[1]);
554 av_freep(&hx->rbsp_buffer[0]);
555 hx->rbsp_buffer_size[0] = 0;
556 hx->rbsp_buffer_size[1] = 0;
557 if (i) av_freep(&h->thread_context[i]);
561 static void init_dequant8_coeff_table(H264Context *h){
563 h->dequant8_coeff[0] = h->dequant8_buffer[0];
564 h->dequant8_coeff[1] = h->dequant8_buffer[1];
567 if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
568 h->dequant8_coeff[1] = h->dequant8_buffer[0];
576 h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
577 ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
578 h->pps.scaling_matrix8[i][x]) << shift;
583 static void init_dequant4_coeff_table(H264Context *h){
586 h->dequant4_coeff[i] = h->dequant4_buffer[i];
588 if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
589 h->dequant4_coeff[i] = h->dequant4_buffer[j];
597 int shift = div6[q] + 2;
600 h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
601 ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
602 h->pps.scaling_matrix4[i][x]) << shift;
607 static void init_dequant_tables(H264Context *h){
609 init_dequant4_coeff_table(h);
610 if(h->pps.transform_8x8_mode)
611 init_dequant8_coeff_table(h);
612 if(h->sps.transform_bypass){
615 h->dequant4_coeff[i][0][x] = 1<<6;
616 if(h->pps.transform_8x8_mode)
619 h->dequant8_coeff[i][0][x] = 1<<6;
624 int ff_h264_alloc_tables(H264Context *h){
625 MpegEncContext * const s = &h->s;
626 const int big_mb_num= s->mb_stride * (s->mb_height+1);
627 const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
630 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8 * sizeof(uint8_t), fail)
632 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 32 * sizeof(uint8_t), fail)
633 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
634 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
636 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
637 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
638 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
639 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
640 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
642 memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
643 h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
645 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
646 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
647 for(y=0; y<s->mb_height; y++){
648 for(x=0; x<s->mb_width; x++){
649 const int mb_xy= x + y*s->mb_stride;
650 const int b_xy = 4*x + 4*y*h->b_stride;
652 h->mb2b_xy [mb_xy]= b_xy;
653 h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
657 s->obmc_scratchpad = NULL;
659 if(!h->dequant4_coeff[0])
660 init_dequant_tables(h);
669 * Mimic alloc_tables(), but for every context thread.
671 static void clone_tables(H264Context *dst, H264Context *src, int i){
672 MpegEncContext * const s = &src->s;
673 dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
674 dst->non_zero_count = src->non_zero_count;
675 dst->slice_table = src->slice_table;
676 dst->cbp_table = src->cbp_table;
677 dst->mb2b_xy = src->mb2b_xy;
678 dst->mb2br_xy = src->mb2br_xy;
679 dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
680 dst->mvd_table[0] = src->mvd_table[0] + i*8*2*s->mb_stride;
681 dst->mvd_table[1] = src->mvd_table[1] + i*8*2*s->mb_stride;
682 dst->direct_table = src->direct_table;
683 dst->list_counts = src->list_counts;
685 dst->s.obmc_scratchpad = NULL;
686 ff_h264_pred_init(&dst->hpc, src->s.codec_id);
691 * Allocate buffers which are not shared amongst multiple threads.
693 static int context_init(H264Context *h){
694 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
695 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
697 h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
698 h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
702 return -1; // free_tables will clean up for us
705 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
707 static av_cold void common_init(H264Context *h){
708 MpegEncContext * const s = &h->s;
710 s->width = s->avctx->width;
711 s->height = s->avctx->height;
712 s->codec_id= s->avctx->codec->id;
714 ff_h264dsp_init(&h->h264dsp);
715 ff_h264_pred_init(&h->hpc, s->codec_id);
717 h->dequant_coeff_pps= -1;
718 s->unrestricted_mv=1;
721 dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
723 memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
724 memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
727 int ff_h264_decode_extradata(H264Context *h)
729 AVCodecContext *avctx = h->s.avctx;
731 if(*(char *)avctx->extradata == 1){
733 unsigned char *p = avctx->extradata;
737 if(avctx->extradata_size < 7) {
738 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
741 /* sps and pps in the avcC always have length coded with 2 bytes,
742 so put a fake nal_length_size = 2 while parsing them */
743 h->nal_length_size = 2;
744 // Decode sps from avcC
745 cnt = *(p+5) & 0x1f; // Number of sps
747 for (i = 0; i < cnt; i++) {
748 nalsize = AV_RB16(p) + 2;
749 if(decode_nal_units(h, p, nalsize) < 0) {
750 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
755 // Decode pps from avcC
756 cnt = *(p++); // Number of pps
757 for (i = 0; i < cnt; i++) {
758 nalsize = AV_RB16(p) + 2;
759 if(decode_nal_units(h, p, nalsize) != nalsize) {
760 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
765 // Now store right nal length size, that will be use to parse all other nals
766 h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
769 if(decode_nal_units(h, avctx->extradata, avctx->extradata_size) < 0)
775 av_cold int ff_h264_decode_init(AVCodecContext *avctx){
776 H264Context *h= avctx->priv_data;
777 MpegEncContext * const s = &h->s;
779 MPV_decode_defaults(s);
784 s->out_format = FMT_H264;
785 s->workaround_bugs= avctx->workaround_bugs;
788 // s->decode_mb= ff_h263_decode_mb;
789 s->quarter_sample = 1;
790 if(!avctx->has_b_frames)
793 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
795 ff_h264_decode_init_vlc();
797 h->thread_context[0] = h;
798 h->outputed_poc = INT_MIN;
799 h->prev_poc_msb= 1<<16;
801 ff_h264_reset_sei(h);
802 if(avctx->codec_id == CODEC_ID_H264){
803 if(avctx->ticks_per_frame == 1){
804 s->avctx->time_base.den *=2;
806 avctx->ticks_per_frame = 2;
809 if(avctx->extradata_size > 0 && avctx->extradata &&
810 ff_h264_decode_extradata(h))
813 if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
814 s->avctx->has_b_frames = h->sps.num_reorder_frames;
821 int ff_h264_frame_start(H264Context *h){
822 MpegEncContext * const s = &h->s;
825 if(MPV_frame_start(s, s->avctx) < 0)
827 ff_er_frame_start(s);
829 * MPV_frame_start uses pict_type to derive key_frame.
830 * This is incorrect for H.264; IDR markings must be used.
831 * Zero here; IDR markings per slice in frame or fields are ORed in later.
832 * See decode_nal_units().
834 s->current_picture_ptr->key_frame= 0;
835 s->current_picture_ptr->mmco_reset= 0;
837 assert(s->linesize && s->uvlinesize);
840 h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
841 h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
844 h->block_offset[16+i]=
845 h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
846 h->block_offset[24+16+i]=
847 h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
850 /* can't be in alloc_tables because linesize isn't known there.
851 * FIXME: redo bipred weight to not require extra buffer? */
852 for(i = 0; i < s->avctx->thread_count; i++)
853 if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
854 h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
856 /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
857 memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
859 // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
861 // We mark the current picture as non-reference after allocating it, so
862 // that if we break out due to an error it can be released automatically
863 // in the next MPV_frame_start().
864 // SVQ3 as well as most other codecs have only last/next/current and thus
865 // get released even with set reference, besides SVQ3 and others do not
866 // mark frames as reference later "naturally".
867 if(s->codec_id != CODEC_ID_SVQ3)
868 s->current_picture_ptr->reference= 0;
870 s->current_picture_ptr->field_poc[0]=
871 s->current_picture_ptr->field_poc[1]= INT_MAX;
872 assert(s->current_picture_ptr->long_ref==0);
877 static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
878 MpegEncContext * const s = &h->s;
883 src_cb -= uvlinesize;
884 src_cr -= uvlinesize;
886 if(!simple && FRAME_MBAFF){
889 top_border = h->top_borders[0][s->mb_x];
890 AV_COPY128(top_border, src_y + 15*linesize);
891 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
892 AV_COPY64(top_border+16, src_cb+7*uvlinesize);
893 AV_COPY64(top_border+24, src_cr+7*uvlinesize);
902 top_border = h->top_borders[top_idx][s->mb_x];
903 // There are two lines saved, the line above the the top macroblock of a pair,
904 // and the line above the bottom macroblock
905 AV_COPY128(top_border, src_y + 16*linesize);
907 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
908 AV_COPY64(top_border+16, src_cb+8*uvlinesize);
909 AV_COPY64(top_border+24, src_cr+8*uvlinesize);
913 static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
914 MpegEncContext * const s = &h->s;
918 uint8_t *top_border_m1;
921 if(!simple && FRAME_MBAFF){
926 top_idx = MB_MBAFF ? 0 : 1;
930 if(h->deblocking_filter == 2) {
931 deblock_left = h->left_type[0];
932 deblock_top = h->top_type;
934 deblock_left = (s->mb_x > 0);
935 deblock_top = (s->mb_y > !!MB_FIELD);
938 src_y -= linesize + 1;
939 src_cb -= uvlinesize + 1;
940 src_cr -= uvlinesize + 1;
942 top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
943 top_border = h->top_borders[top_idx][s->mb_x];
945 #define XCHG(a,b,xchg)\
946 if (xchg) AV_SWAP64(b,a);\
951 XCHG(top_border_m1+8, src_y -7, 1);
953 XCHG(top_border+0, src_y +1, xchg);
954 XCHG(top_border+8, src_y +9, 1);
955 if(s->mb_x+1 < s->mb_width){
956 XCHG(h->top_borders[top_idx][s->mb_x+1], src_y +17, 1);
960 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
963 XCHG(top_border_m1+16, src_cb -7, 1);
964 XCHG(top_border_m1+24, src_cr -7, 1);
966 XCHG(top_border+16, src_cb+1, 1);
967 XCHG(top_border+24, src_cr+1, 1);
972 static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
973 MpegEncContext * const s = &h->s;
974 const int mb_x= s->mb_x;
975 const int mb_y= s->mb_y;
976 const int mb_xy= h->mb_xy;
977 const int mb_type= s->current_picture.mb_type[mb_xy];
978 uint8_t *dest_y, *dest_cb, *dest_cr;
979 int linesize, uvlinesize /*dct_offset*/;
981 int *block_offset = &h->block_offset[0];
982 const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
983 /* is_h264 should always be true if SVQ3 is disabled. */
984 const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
985 void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
986 void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
988 dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
989 dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
990 dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
992 s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
993 s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
995 h->list_counts[mb_xy]= h->list_count;
997 if (!simple && MB_FIELD) {
998 linesize = h->mb_linesize = s->linesize * 2;
999 uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
1000 block_offset = &h->block_offset[24];
1001 if(mb_y&1){ //FIXME move out of this function?
1002 dest_y -= s->linesize*15;
1003 dest_cb-= s->uvlinesize*7;
1004 dest_cr-= s->uvlinesize*7;
1008 for(list=0; list<h->list_count; list++){
1009 if(!USES_LIST(mb_type, list))
1011 if(IS_16X16(mb_type)){
1012 int8_t *ref = &h->ref_cache[list][scan8[0]];
1013 fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
1015 for(i=0; i<16; i+=4){
1016 int ref = h->ref_cache[list][scan8[i]];
1018 fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
1024 linesize = h->mb_linesize = s->linesize;
1025 uvlinesize = h->mb_uvlinesize = s->uvlinesize;
1026 // dct_offset = s->linesize * 16;
1029 if (!simple && IS_INTRA_PCM(mb_type)) {
1030 for (i=0; i<16; i++) {
1031 memcpy(dest_y + i* linesize, h->mb + i*8, 16);
1033 for (i=0; i<8; i++) {
1034 memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
1035 memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
1038 if(IS_INTRA(mb_type)){
1039 if(h->deblocking_filter)
1040 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
1042 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1043 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
1044 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
1047 if(IS_INTRA4x4(mb_type)){
1048 if(simple || !s->encoding){
1049 if(IS_8x8DCT(mb_type)){
1050 if(transform_bypass){
1052 idct_add = s->dsp.add_pixels8;
1054 idct_dc_add = h->h264dsp.h264_idct8_dc_add;
1055 idct_add = h->h264dsp.h264_idct8_add;
1057 for(i=0; i<16; i+=4){
1058 uint8_t * const ptr= dest_y + block_offset[i];
1059 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
1060 if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
1061 h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
1063 const int nnz = h->non_zero_count_cache[ scan8[i] ];
1064 h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
1065 (h->topright_samples_available<<i)&0x4000, linesize);
1067 if(nnz == 1 && h->mb[i*16])
1068 idct_dc_add(ptr, h->mb + i*16, linesize);
1070 idct_add (ptr, h->mb + i*16, linesize);
1075 if(transform_bypass){
1077 idct_add = s->dsp.add_pixels4;
1079 idct_dc_add = h->h264dsp.h264_idct_dc_add;
1080 idct_add = h->h264dsp.h264_idct_add;
1082 for(i=0; i<16; i++){
1083 uint8_t * const ptr= dest_y + block_offset[i];
1084 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
1086 if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
1087 h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
1091 if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
1092 const int topright_avail= (h->topright_samples_available<<i)&0x8000;
1093 assert(mb_y || linesize <= block_offset[i]);
1094 if(!topright_avail){
1095 tr= ptr[3 - linesize]*0x01010101;
1096 topright= (uint8_t*) &tr;
1098 topright= ptr + 4 - linesize;
1102 h->hpc.pred4x4[ dir ](ptr, topright, linesize);
1103 nnz = h->non_zero_count_cache[ scan8[i] ];
1106 if(nnz == 1 && h->mb[i*16])
1107 idct_dc_add(ptr, h->mb + i*16, linesize);
1109 idct_add (ptr, h->mb + i*16, linesize);
1111 ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
1118 h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
1120 if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX] ]){
1121 if(!transform_bypass)
1122 h->h264dsp.h264_luma_dc_dequant_idct(h->mb, h->mb_luma_dc, h->dequant4_coeff[0][s->qscale][0]);
1124 static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
1125 8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
1126 for(i = 0; i < 16; i++)
1127 h->mb[dc_mapping[i]] = h->mb_luma_dc[i];
1131 ff_svq3_luma_dc_dequant_idct_c(h->mb, h->mb_luma_dc, s->qscale);
1133 if(h->deblocking_filter)
1134 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
1136 hl_motion(h, dest_y, dest_cb, dest_cr,
1137 s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
1138 s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
1139 h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
1143 if(!IS_INTRA4x4(mb_type)){
1145 if(IS_INTRA16x16(mb_type)){
1146 if(transform_bypass){
1147 if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
1148 h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
1150 for(i=0; i<16; i++){
1151 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
1152 s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
1156 h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
1158 }else if(h->cbp&15){
1159 if(transform_bypass){
1160 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
1161 idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
1162 for(i=0; i<16; i+=di){
1163 if(h->non_zero_count_cache[ scan8[i] ]){
1164 idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
1168 if(IS_8x8DCT(mb_type)){
1169 h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
1171 h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
1176 for(i=0; i<16; i++){
1177 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
1178 uint8_t * const ptr= dest_y + block_offset[i];
1179 ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
1185 if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
1186 uint8_t *dest[2] = {dest_cb, dest_cr};
1187 if(transform_bypass){
1188 if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
1189 h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
1190 h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
1192 idct_add = s->dsp.add_pixels4;
1193 for(i=16; i<16+8; i++){
1194 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
1195 idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
1199 int chroma_qpu = h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0];
1200 int chroma_qpv = h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0];
1202 if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
1203 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+0*16, &h->mb_chroma_dc[0], chroma_qpu );
1204 if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
1205 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, &h->mb_chroma_dc[1], chroma_qpv );
1206 h->h264dsp.h264_idct_add8(dest, block_offset,
1208 h->non_zero_count_cache);
1210 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+0*16, &h->mb_chroma_dc[0], chroma_qpu );
1211 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, &h->mb_chroma_dc[1], chroma_qpv );
1212 for(i=16; i<16+8; i++){
1213 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
1214 uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
1215 ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
1222 if(h->cbp || IS_INTRA(mb_type))
1223 s->dsp.clear_blocks(h->mb);
1227 * Process a macroblock; this case avoids checks for expensive uncommon cases.
1229 static void hl_decode_mb_simple(H264Context *h){
1230 hl_decode_mb_internal(h, 1);
1234 * Process a macroblock; this handles edge cases, such as interlacing.
1236 static void av_noinline hl_decode_mb_complex(H264Context *h){
1237 hl_decode_mb_internal(h, 0);
1240 void ff_h264_hl_decode_mb(H264Context *h){
1241 MpegEncContext * const s = &h->s;
1242 const int mb_xy= h->mb_xy;
1243 const int mb_type= s->current_picture.mb_type[mb_xy];
1244 int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
1247 hl_decode_mb_complex(h);
1248 else hl_decode_mb_simple(h);
1251 static int pred_weight_table(H264Context *h){
1252 MpegEncContext * const s = &h->s;
1254 int luma_def, chroma_def;
1257 h->use_weight_chroma= 0;
1258 h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
1260 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
1261 luma_def = 1<<h->luma_log2_weight_denom;
1262 chroma_def = 1<<h->chroma_log2_weight_denom;
1264 for(list=0; list<2; list++){
1265 h->luma_weight_flag[list] = 0;
1266 h->chroma_weight_flag[list] = 0;
1267 for(i=0; i<h->ref_count[list]; i++){
1268 int luma_weight_flag, chroma_weight_flag;
1270 luma_weight_flag= get_bits1(&s->gb);
1271 if(luma_weight_flag){
1272 h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
1273 h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
1274 if( h->luma_weight[i][list][0] != luma_def
1275 || h->luma_weight[i][list][1] != 0) {
1277 h->luma_weight_flag[list]= 1;
1280 h->luma_weight[i][list][0]= luma_def;
1281 h->luma_weight[i][list][1]= 0;
1285 chroma_weight_flag= get_bits1(&s->gb);
1286 if(chroma_weight_flag){
1289 h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
1290 h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
1291 if( h->chroma_weight[i][list][j][0] != chroma_def
1292 || h->chroma_weight[i][list][j][1] != 0) {
1293 h->use_weight_chroma= 1;
1294 h->chroma_weight_flag[list]= 1;
1300 h->chroma_weight[i][list][j][0]= chroma_def;
1301 h->chroma_weight[i][list][j][1]= 0;
1306 if(h->slice_type_nos != FF_B_TYPE) break;
1308 h->use_weight= h->use_weight || h->use_weight_chroma;
1313 * Initialize implicit_weight table.
1314 * @param field 0/1 initialize the weight for interlaced MBAFF
1315 * -1 initializes the rest
1317 static void implicit_weight_table(H264Context *h, int field){
1318 MpegEncContext * const s = &h->s;
1319 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
1321 for (i = 0; i < 2; i++) {
1322 h->luma_weight_flag[i] = 0;
1323 h->chroma_weight_flag[i] = 0;
1327 cur_poc = s->current_picture_ptr->poc;
1328 if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
1329 && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
1331 h->use_weight_chroma= 0;
1335 ref_count0= h->ref_count[0];
1336 ref_count1= h->ref_count[1];
1338 cur_poc = s->current_picture_ptr->field_poc[field];
1340 ref_count0= 16+2*h->ref_count[0];
1341 ref_count1= 16+2*h->ref_count[1];
1345 h->use_weight_chroma= 2;
1346 h->luma_log2_weight_denom= 5;
1347 h->chroma_log2_weight_denom= 5;
1349 for(ref0=ref_start; ref0 < ref_count0; ref0++){
1350 int poc0 = h->ref_list[0][ref0].poc;
1351 for(ref1=ref_start; ref1 < ref_count1; ref1++){
1352 int poc1 = h->ref_list[1][ref1].poc;
1353 int td = av_clip(poc1 - poc0, -128, 127);
1356 int tb = av_clip(cur_poc - poc0, -128, 127);
1357 int tx = (16384 + (FFABS(td) >> 1)) / td;
1358 int dist_scale_factor = (tb*tx + 32) >> 8;
1359 if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
1360 w = 64 - dist_scale_factor;
1363 h->implicit_weight[ref0][ref1][0]=
1364 h->implicit_weight[ref0][ref1][1]= w;
1366 h->implicit_weight[ref0][ref1][field]=w;
1373 * instantaneous decoder refresh.
1375 static void idr(H264Context *h){
1376 ff_h264_remove_all_refs(h);
1377 h->prev_frame_num= 0;
1378 h->prev_frame_num_offset= 0;
1383 /* forget old pics after a seek */
1384 static void flush_dpb(AVCodecContext *avctx){
1385 H264Context *h= avctx->priv_data;
1387 for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
1388 if(h->delayed_pic[i])
1389 h->delayed_pic[i]->reference= 0;
1390 h->delayed_pic[i]= NULL;
1392 h->outputed_poc= INT_MIN;
1393 h->prev_interlaced_frame = 1;
1395 if(h->s.current_picture_ptr)
1396 h->s.current_picture_ptr->reference= 0;
1397 h->s.first_field= 0;
1398 ff_h264_reset_sei(h);
1399 ff_mpeg_flush(avctx);
1402 static int init_poc(H264Context *h){
1403 MpegEncContext * const s = &h->s;
1404 const int max_frame_num= 1<<h->sps.log2_max_frame_num;
1406 Picture *cur = s->current_picture_ptr;
1408 h->frame_num_offset= h->prev_frame_num_offset;
1409 if(h->frame_num < h->prev_frame_num)
1410 h->frame_num_offset += max_frame_num;
1412 if(h->sps.poc_type==0){
1413 const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
1415 if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
1416 h->poc_msb = h->prev_poc_msb + max_poc_lsb;
1417 else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
1418 h->poc_msb = h->prev_poc_msb - max_poc_lsb;
1420 h->poc_msb = h->prev_poc_msb;
1421 //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
1423 field_poc[1] = h->poc_msb + h->poc_lsb;
1424 if(s->picture_structure == PICT_FRAME)
1425 field_poc[1] += h->delta_poc_bottom;
1426 }else if(h->sps.poc_type==1){
1427 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
1430 if(h->sps.poc_cycle_length != 0)
1431 abs_frame_num = h->frame_num_offset + h->frame_num;
1435 if(h->nal_ref_idc==0 && abs_frame_num > 0)
1438 expected_delta_per_poc_cycle = 0;
1439 for(i=0; i < h->sps.poc_cycle_length; i++)
1440 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
1442 if(abs_frame_num > 0){
1443 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
1444 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
1446 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
1447 for(i = 0; i <= frame_num_in_poc_cycle; i++)
1448 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
1452 if(h->nal_ref_idc == 0)
1453 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
1455 field_poc[0] = expectedpoc + h->delta_poc[0];
1456 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
1458 if(s->picture_structure == PICT_FRAME)
1459 field_poc[1] += h->delta_poc[1];
1461 int poc= 2*(h->frame_num_offset + h->frame_num);
1470 if(s->picture_structure != PICT_BOTTOM_FIELD)
1471 s->current_picture_ptr->field_poc[0]= field_poc[0];
1472 if(s->picture_structure != PICT_TOP_FIELD)
1473 s->current_picture_ptr->field_poc[1]= field_poc[1];
1474 cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
1481 * initialize scan tables
1483 static void init_scan_tables(H264Context *h){
1485 for(i=0; i<16; i++){
1486 #define T(x) (x>>2) | ((x<<2) & 0xF)
1487 h->zigzag_scan[i] = T(zigzag_scan[i]);
1488 h-> field_scan[i] = T( field_scan[i]);
1491 for(i=0; i<64; i++){
1492 #define T(x) (x>>3) | ((x&7)<<3)
1493 h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
1494 h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
1495 h->field_scan8x8[i] = T(field_scan8x8[i]);
1496 h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
1499 if(h->sps.transform_bypass){ //FIXME same ugly
1500 h->zigzag_scan_q0 = zigzag_scan;
1501 h->zigzag_scan8x8_q0 = ff_zigzag_direct;
1502 h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
1503 h->field_scan_q0 = field_scan;
1504 h->field_scan8x8_q0 = field_scan8x8;
1505 h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
1507 h->zigzag_scan_q0 = h->zigzag_scan;
1508 h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
1509 h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
1510 h->field_scan_q0 = h->field_scan;
1511 h->field_scan8x8_q0 = h->field_scan8x8;
1512 h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
1516 static void field_end(H264Context *h){
1517 MpegEncContext * const s = &h->s;
1518 AVCodecContext * const avctx= s->avctx;
1521 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
1522 s->current_picture_ptr->pict_type= s->pict_type;
1524 if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
1525 ff_vdpau_h264_set_reference_frames(s);
1528 ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1529 h->prev_poc_msb= h->poc_msb;
1530 h->prev_poc_lsb= h->poc_lsb;
1532 h->prev_frame_num_offset= h->frame_num_offset;
1533 h->prev_frame_num= h->frame_num;
1535 if (avctx->hwaccel) {
1536 if (avctx->hwaccel->end_frame(avctx) < 0)
1537 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1540 if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
1541 ff_vdpau_h264_picture_complete(s);
1544 * FIXME: Error handling code does not seem to support interlaced
1545 * when slices span multiple rows
1546 * The ff_er_add_slice calls don't work right for bottom
1547 * fields; they cause massive erroneous error concealing
1548 * Error marking covers both fields (top and bottom).
1549 * This causes a mismatched s->error_count
1550 * and a bad error table. Further, the error count goes to
1551 * INT_MAX when called for bottom field, because mb_y is
1552 * past end by one (callers fault) and resync_mb_y != 0
1553 * causes problems for the first MB line, too.
1564 * Replicate H264 "master" context to thread contexts.
1566 static void clone_slice(H264Context *dst, H264Context *src)
1568 memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
1569 dst->s.current_picture_ptr = src->s.current_picture_ptr;
1570 dst->s.current_picture = src->s.current_picture;
1571 dst->s.linesize = src->s.linesize;
1572 dst->s.uvlinesize = src->s.uvlinesize;
1573 dst->s.first_field = src->s.first_field;
1575 dst->prev_poc_msb = src->prev_poc_msb;
1576 dst->prev_poc_lsb = src->prev_poc_lsb;
1577 dst->prev_frame_num_offset = src->prev_frame_num_offset;
1578 dst->prev_frame_num = src->prev_frame_num;
1579 dst->short_ref_count = src->short_ref_count;
1581 memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
1582 memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
1583 memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
1584 memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
1586 memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
1587 memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
1591 * decodes a slice header.
1592 * This will also call MPV_common_init() and frame_start() as needed.
1594 * @param h h264context
1595 * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
1597 * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
1599 static int decode_slice_header(H264Context *h, H264Context *h0){
1600 MpegEncContext * const s = &h->s;
1601 MpegEncContext * const s0 = &h0->s;
1602 unsigned int first_mb_in_slice;
1603 unsigned int pps_id;
1604 int num_ref_idx_active_override_flag;
1605 unsigned int slice_type, tmp, i, j;
1606 int default_ref_list_done = 0;
1607 int last_pic_structure;
1609 s->dropable= h->nal_ref_idc == 0;
1611 if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
1612 s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
1613 s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
1615 s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
1616 s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
1619 first_mb_in_slice= get_ue_golomb(&s->gb);
1621 if(first_mb_in_slice == 0){ //FIXME better field boundary detection
1622 if(h0->current_slice && FIELD_PICTURE){
1626 h0->current_slice = 0;
1627 if (!s0->first_field)
1628 s->current_picture_ptr= NULL;
1631 slice_type= get_ue_golomb_31(&s->gb);
1633 av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
1638 h->slice_type_fixed=1;
1640 h->slice_type_fixed=0;
1642 slice_type= golomb_to_pict_type[ slice_type ];
1643 if (slice_type == FF_I_TYPE
1644 || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
1645 default_ref_list_done = 1;
1647 h->slice_type= slice_type;
1648 h->slice_type_nos= slice_type & 3;
1650 s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
1652 pps_id= get_ue_golomb(&s->gb);
1653 if(pps_id>=MAX_PPS_COUNT){
1654 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
1657 if(!h0->pps_buffers[pps_id]) {
1658 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
1661 h->pps= *h0->pps_buffers[pps_id];
1663 if(!h0->sps_buffers[h->pps.sps_id]) {
1664 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
1667 h->sps = *h0->sps_buffers[h->pps.sps_id];
1669 s->avctx->profile = h->sps.profile_idc;
1670 s->avctx->level = h->sps.level_idc;
1671 s->avctx->refs = h->sps.ref_frame_count;
1673 if(h == h0 && h->dequant_coeff_pps != pps_id){
1674 h->dequant_coeff_pps = pps_id;
1675 init_dequant_tables(h);
1678 s->mb_width= h->sps.mb_width;
1679 s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
1681 h->b_stride= s->mb_width*4;
1683 s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
1684 if(h->sps.frame_mbs_only_flag)
1685 s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
1687 s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 7);
1689 if (s->context_initialized
1690 && ( s->width != s->avctx->width || s->height != s->avctx->height
1691 || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
1693 return -1; // width / height changed during parallelized decoding
1695 flush_dpb(s->avctx);
1698 if (!s->context_initialized) {
1700 return -1; // we cant (re-)initialize context during parallel decoding
1702 avcodec_set_dimensions(s->avctx, s->width, s->height);
1703 s->avctx->sample_aspect_ratio= h->sps.sar;
1704 av_assert0(s->avctx->sample_aspect_ratio.den);
1706 if(h->sps.video_signal_type_present_flag){
1707 s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
1708 if(h->sps.colour_description_present_flag){
1709 s->avctx->color_primaries = h->sps.color_primaries;
1710 s->avctx->color_trc = h->sps.color_trc;
1711 s->avctx->colorspace = h->sps.colorspace;
1715 if(h->sps.timing_info_present_flag){
1716 int64_t den= h->sps.time_scale;
1717 if(h->x264_build < 44U)
1719 av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
1720 h->sps.num_units_in_tick, den, 1<<30);
1722 s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
1723 s->avctx->codec->pix_fmts ?
1724 s->avctx->codec->pix_fmts :
1725 s->avctx->color_range == AVCOL_RANGE_JPEG ?
1726 hwaccel_pixfmt_list_h264_jpeg_420 :
1727 ff_hwaccel_pixfmt_list_420);
1728 s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
1730 if (MPV_common_init(s) < 0)
1733 h->prev_interlaced_frame = 1;
1735 init_scan_tables(h);
1736 ff_h264_alloc_tables(h);
1738 for(i = 1; i < s->avctx->thread_count; i++) {
1740 c = h->thread_context[i] = av_malloc(sizeof(H264Context));
1741 memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
1742 memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
1743 c->h264dsp = h->h264dsp;
1746 init_scan_tables(c);
1747 clone_tables(c, h, i);
1750 for(i = 0; i < s->avctx->thread_count; i++)
1751 if(context_init(h->thread_context[i]) < 0)
1755 h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
1758 h->mb_aff_frame = 0;
1759 last_pic_structure = s0->picture_structure;
1760 if(h->sps.frame_mbs_only_flag){
1761 s->picture_structure= PICT_FRAME;
1763 if(get_bits1(&s->gb)) { //field_pic_flag
1764 s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
1766 s->picture_structure= PICT_FRAME;
1767 h->mb_aff_frame = h->sps.mb_aff;
1770 h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
1772 if(h0->current_slice == 0){
1773 while(h->frame_num != h->prev_frame_num &&
1774 h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
1775 Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
1776 av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
1777 if (ff_h264_frame_start(h) < 0)
1779 h->prev_frame_num++;
1780 h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
1781 s->current_picture_ptr->frame_num= h->prev_frame_num;
1782 ff_generate_sliding_window_mmcos(h);
1783 ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1784 /* Error concealment: if a ref is missing, copy the previous ref in its place.
1785 * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
1786 * about there being no actual duplicates.
1787 * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
1788 * concealing a lost frame, this probably isn't noticable by comparison, but it should
1790 if (h->short_ref_count) {
1792 av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
1793 (const uint8_t**)prev->data, prev->linesize,
1794 s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
1795 h->short_ref[0]->poc = prev->poc+2;
1797 h->short_ref[0]->frame_num = h->prev_frame_num;
1801 /* See if we have a decoded first field looking for a pair... */
1802 if (s0->first_field) {
1803 assert(s0->current_picture_ptr);
1804 assert(s0->current_picture_ptr->data[0]);
1805 assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
1807 /* figure out if we have a complementary field pair */
1808 if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
1810 * Previous field is unmatched. Don't display it, but let it
1811 * remain for reference if marked as such.
1813 s0->current_picture_ptr = NULL;
1814 s0->first_field = FIELD_PICTURE;
1817 if (h->nal_ref_idc &&
1818 s0->current_picture_ptr->reference &&
1819 s0->current_picture_ptr->frame_num != h->frame_num) {
1821 * This and previous field were reference, but had
1822 * different frame_nums. Consider this field first in
1823 * pair. Throw away previous field except for reference
1826 s0->first_field = 1;
1827 s0->current_picture_ptr = NULL;
1830 /* Second field in complementary pair */
1831 s0->first_field = 0;
1836 /* Frame or first field in a potentially complementary pair */
1837 assert(!s0->current_picture_ptr);
1838 s0->first_field = FIELD_PICTURE;
1841 if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
1842 s0->first_field = 0;
1849 s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
1851 assert(s->mb_num == s->mb_width * s->mb_height);
1852 if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
1853 first_mb_in_slice >= s->mb_num){
1854 av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
1857 s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
1858 s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
1859 if (s->picture_structure == PICT_BOTTOM_FIELD)
1860 s->resync_mb_y = s->mb_y = s->mb_y + 1;
1861 assert(s->mb_y < s->mb_height);
1863 if(s->picture_structure==PICT_FRAME){
1864 h->curr_pic_num= h->frame_num;
1865 h->max_pic_num= 1<< h->sps.log2_max_frame_num;
1867 h->curr_pic_num= 2*h->frame_num + 1;
1868 h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
1871 if(h->nal_unit_type == NAL_IDR_SLICE){
1872 get_ue_golomb(&s->gb); /* idr_pic_id */
1875 if(h->sps.poc_type==0){
1876 h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
1878 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
1879 h->delta_poc_bottom= get_se_golomb(&s->gb);
1883 if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
1884 h->delta_poc[0]= get_se_golomb(&s->gb);
1886 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
1887 h->delta_poc[1]= get_se_golomb(&s->gb);
1892 if(h->pps.redundant_pic_cnt_present){
1893 h->redundant_pic_count= get_ue_golomb(&s->gb);
1896 //set defaults, might be overridden a few lines later
1897 h->ref_count[0]= h->pps.ref_count[0];
1898 h->ref_count[1]= h->pps.ref_count[1];
1900 if(h->slice_type_nos != FF_I_TYPE){
1901 if(h->slice_type_nos == FF_B_TYPE){
1902 h->direct_spatial_mv_pred= get_bits1(&s->gb);
1904 num_ref_idx_active_override_flag= get_bits1(&s->gb);
1906 if(num_ref_idx_active_override_flag){
1907 h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
1908 if(h->slice_type_nos==FF_B_TYPE)
1909 h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
1911 if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
1912 av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
1913 h->ref_count[0]= h->ref_count[1]= 1;
1917 if(h->slice_type_nos == FF_B_TYPE)
1924 if(!default_ref_list_done){
1925 ff_h264_fill_default_ref_list(h);
1928 if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
1931 if(h->slice_type_nos!=FF_I_TYPE){
1932 s->last_picture_ptr= &h->ref_list[0][0];
1933 ff_copy_picture(&s->last_picture, s->last_picture_ptr);
1935 if(h->slice_type_nos==FF_B_TYPE){
1936 s->next_picture_ptr= &h->ref_list[1][0];
1937 ff_copy_picture(&s->next_picture, s->next_picture_ptr);
1940 if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
1941 || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
1942 pred_weight_table(h);
1943 else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
1944 implicit_weight_table(h, -1);
1947 for (i = 0; i < 2; i++) {
1948 h->luma_weight_flag[i] = 0;
1949 h->chroma_weight_flag[i] = 0;
1954 ff_h264_decode_ref_pic_marking(h0, &s->gb);
1957 ff_h264_fill_mbaff_ref_list(h);
1959 if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
1960 implicit_weight_table(h, 0);
1961 implicit_weight_table(h, 1);
1965 if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
1966 ff_h264_direct_dist_scale_factor(h);
1967 ff_h264_direct_ref_list_init(h);
1969 if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
1970 tmp = get_ue_golomb_31(&s->gb);
1972 av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
1975 h->cabac_init_idc= tmp;
1978 h->last_qscale_diff = 0;
1979 tmp = h->pps.init_qp + get_se_golomb(&s->gb);
1981 av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
1985 h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
1986 h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
1987 //FIXME qscale / qp ... stuff
1988 if(h->slice_type == FF_SP_TYPE){
1989 get_bits1(&s->gb); /* sp_for_switch_flag */
1991 if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
1992 get_se_golomb(&s->gb); /* slice_qs_delta */
1995 h->deblocking_filter = 1;
1996 h->slice_alpha_c0_offset = 52;
1997 h->slice_beta_offset = 52;
1998 if( h->pps.deblocking_filter_parameters_present ) {
1999 tmp= get_ue_golomb_31(&s->gb);
2001 av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
2004 h->deblocking_filter= tmp;
2005 if(h->deblocking_filter < 2)
2006 h->deblocking_filter^= 1; // 1<->0
2008 if( h->deblocking_filter ) {
2009 h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
2010 h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
2011 if( h->slice_alpha_c0_offset > 104U
2012 || h->slice_beta_offset > 104U){
2013 av_log(s->avctx, AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", h->slice_alpha_c0_offset, h->slice_beta_offset);
2019 if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
2020 ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
2021 ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
2022 ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
2023 h->deblocking_filter= 0;
2025 if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
2026 if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
2027 /* Cheat slightly for speed:
2028 Do not bother to deblock across slices. */
2029 h->deblocking_filter = 2;
2031 h0->max_contexts = 1;
2032 if(!h0->single_decode_warning) {
2033 av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
2034 h0->single_decode_warning = 1;
2037 return 1; // deblocking switched inside frame
2040 h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
2043 if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
2044 slice_group_change_cycle= get_bits(&s->gb, ?);
2047 h0->last_slice_type = slice_type;
2048 h->slice_num = ++h0->current_slice;
2049 if(h->slice_num >= MAX_SLICES){
2050 av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
2055 int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
2056 for(i=0; i<16; i++){
2058 if(h->ref_list[j][i].data[0]){
2060 uint8_t *base= h->ref_list[j][i].base[0];
2061 for(k=0; k<h->short_ref_count; k++)
2062 if(h->short_ref[k]->base[0] == base){
2066 for(k=0; k<h->long_ref_count; k++)
2067 if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
2068 id_list[i]= h->short_ref_count + k;
2077 ref2frm[i+2]= 4*id_list[i]
2078 +(h->ref_list[j][i].reference&3);
2081 for(i=16; i<48; i++)
2082 ref2frm[i+4]= 4*id_list[(i-16)>>1]
2083 +(h->ref_list[j][i].reference&3);
2086 h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
2087 h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
2089 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
2090 av_log(h->s.avctx, AV_LOG_DEBUG, "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",
2092 (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
2094 av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
2095 pps_id, h->frame_num,
2096 s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
2097 h->ref_count[0], h->ref_count[1],
2099 h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
2101 h->use_weight==1 && h->use_weight_chroma ? "c" : "",
2102 h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
2109 int ff_h264_get_slice_type(const H264Context *h)
2111 switch (h->slice_type) {
2112 case FF_P_TYPE: return 0;
2113 case FF_B_TYPE: return 1;
2114 case FF_I_TYPE: return 2;
2115 case FF_SP_TYPE: return 3;
2116 case FF_SI_TYPE: return 4;
2123 * @return non zero if the loop filter can be skiped
2125 static int fill_filter_caches(H264Context *h, int mb_type){
2126 MpegEncContext * const s = &h->s;
2127 const int mb_xy= h->mb_xy;
2128 int top_xy, left_xy[2];
2129 int top_type, left_type[2];
2131 top_xy = mb_xy - (s->mb_stride << MB_FIELD);
2133 //FIXME deblocking could skip the intra and nnz parts.
2135 /* Wow, what a mess, why didn't they simplify the interlacing & intra
2136 * stuff, I can't imagine that these complex rules are worth it. */
2138 left_xy[1] = left_xy[0] = mb_xy-1;
2140 const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
2141 const int curr_mb_field_flag = IS_INTERLACED(mb_type);
2143 if (left_mb_field_flag != curr_mb_field_flag) {
2144 left_xy[0] -= s->mb_stride;
2147 if(curr_mb_field_flag){
2148 top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
2150 if (left_mb_field_flag != curr_mb_field_flag) {
2151 left_xy[1] += s->mb_stride;
2156 h->top_mb_xy = top_xy;
2157 h->left_mb_xy[0] = left_xy[0];
2158 h->left_mb_xy[1] = left_xy[1];
2160 //for sufficiently low qp, filtering wouldn't do anything
2161 //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
2162 int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
2163 int qp = s->current_picture.qscale_table[mb_xy];
2165 && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
2166 && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
2169 if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
2170 && (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
2175 top_type = s->current_picture.mb_type[top_xy] ;
2176 left_type[0] = s->current_picture.mb_type[left_xy[0]];
2177 left_type[1] = s->current_picture.mb_type[left_xy[1]];
2178 if(h->deblocking_filter == 2){
2179 if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
2180 if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
2182 if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
2183 if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
2185 h->top_type = top_type ;
2186 h->left_type[0]= left_type[0];
2187 h->left_type[1]= left_type[1];
2189 if(IS_INTRA(mb_type))
2192 AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
2193 AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
2194 AV_COPY32(&h->non_zero_count_cache[0+8*5], &h->non_zero_count[mb_xy][16]);
2195 AV_COPY32(&h->non_zero_count_cache[4+8*3], &h->non_zero_count[mb_xy][20]);
2196 AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
2198 h->cbp= h->cbp_table[mb_xy];
2202 for(list=0; list<h->list_count; list++){
2205 int16_t (*mv_dst)[2];
2206 int16_t (*mv_src)[2];
2208 if(!USES_LIST(mb_type, list)){
2209 fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
2210 AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
2211 AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
2212 AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
2213 AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
2217 ref = &s->current_picture.ref_index[list][4*mb_xy];
2219 int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
2220 AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
2221 AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
2223 AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
2224 AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
2227 b_stride = h->b_stride;
2228 mv_dst = &h->mv_cache[list][scan8[0]];
2229 mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
2231 AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
2246 //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
2248 AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][4+3*8]);
2252 h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][7+0*8];
2253 h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7+1*8];
2254 h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][7+2*8];
2255 h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][7+3*8];
2258 // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
2259 if(!CABAC && h->pps.transform_8x8_mode){
2260 if(IS_8x8DCT(top_type)){
2261 h->non_zero_count_cache[4+8*0]=
2262 h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
2263 h->non_zero_count_cache[6+8*0]=
2264 h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
2266 if(IS_8x8DCT(left_type[0])){
2267 h->non_zero_count_cache[3+8*1]=
2268 h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2; //FIXME check MBAFF
2270 if(IS_8x8DCT(left_type[1])){
2271 h->non_zero_count_cache[3+8*3]=
2272 h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8; //FIXME check MBAFF
2275 if(IS_8x8DCT(mb_type)){
2276 h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
2277 h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
2279 h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
2280 h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
2282 h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
2283 h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
2285 h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
2286 h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
2290 if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
2292 for(list=0; list<h->list_count; list++){
2293 if(USES_LIST(top_type, list)){
2294 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
2295 const int b8_xy= 4*top_xy + 2;
2296 int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
2297 AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
2298 h->ref_cache[list][scan8[0] + 0 - 1*8]=
2299 h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
2300 h->ref_cache[list][scan8[0] + 2 - 1*8]=
2301 h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
2303 AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
2304 AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
2307 if(!IS_INTERLACED(mb_type^left_type[0])){
2308 if(USES_LIST(left_type[0], list)){
2309 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
2310 const int b8_xy= 4*left_xy[0] + 1;
2311 int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
2312 AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
2313 AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
2314 AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
2315 AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
2316 h->ref_cache[list][scan8[0] - 1 + 0 ]=
2317 h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
2318 h->ref_cache[list][scan8[0] - 1 +16 ]=
2319 h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
2321 AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
2322 AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
2323 AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
2324 AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
2325 h->ref_cache[list][scan8[0] - 1 + 0 ]=
2326 h->ref_cache[list][scan8[0] - 1 + 8 ]=
2327 h->ref_cache[list][scan8[0] - 1 + 16 ]=
2328 h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
2337 static void loop_filter(H264Context *h){
2338 MpegEncContext * const s = &h->s;
2339 uint8_t *dest_y, *dest_cb, *dest_cr;
2340 int linesize, uvlinesize, mb_x, mb_y;
2341 const int end_mb_y= s->mb_y + FRAME_MBAFF;
2342 const int old_slice_type= h->slice_type;
2344 if(h->deblocking_filter) {
2345 for(mb_x= 0; mb_x<s->mb_width; mb_x++){
2346 for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
2348 mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
2349 h->slice_num= h->slice_table[mb_xy];
2350 mb_type= s->current_picture.mb_type[mb_xy];
2351 h->list_count= h->list_counts[mb_xy];
2354 h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
2358 dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
2359 dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
2360 dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
2361 //FIXME simplify above
2364 linesize = h->mb_linesize = s->linesize * 2;
2365 uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
2366 if(mb_y&1){ //FIXME move out of this function?
2367 dest_y -= s->linesize*15;
2368 dest_cb-= s->uvlinesize*7;
2369 dest_cr-= s->uvlinesize*7;
2372 linesize = h->mb_linesize = s->linesize;
2373 uvlinesize = h->mb_uvlinesize = s->uvlinesize;
2375 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
2376 if(fill_filter_caches(h, mb_type))
2378 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
2379 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
2382 ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
2384 ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
2389 h->slice_type= old_slice_type;
2391 s->mb_y= end_mb_y - FRAME_MBAFF;
2392 h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
2393 h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
2396 static void predict_field_decoding_flag(H264Context *h){
2397 MpegEncContext * const s = &h->s;
2398 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
2399 int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
2400 ? s->current_picture.mb_type[mb_xy-1]
2401 : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
2402 ? s->current_picture.mb_type[mb_xy-s->mb_stride]
2404 h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
2407 static int decode_slice(struct AVCodecContext *avctx, void *arg){
2408 H264Context *h = *(void**)arg;
2409 MpegEncContext * const s = &h->s;
2410 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
2414 h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
2415 (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
2417 if( h->pps.cabac ) {
2419 align_get_bits( &s->gb );
2422 ff_init_cabac_states( &h->cabac);
2423 ff_init_cabac_decoder( &h->cabac,
2424 s->gb.buffer + get_bits_count(&s->gb)/8,
2425 (get_bits_left(&s->gb) + 7)/8);
2427 ff_h264_init_cabac_states(h);
2431 int ret = ff_h264_decode_mb_cabac(h);
2433 //STOP_TIMER("decode_mb_cabac")
2435 if(ret>=0) ff_h264_hl_decode_mb(h);
2437 if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
2440 ret = ff_h264_decode_mb_cabac(h);
2442 if(ret>=0) ff_h264_hl_decode_mb(h);
2445 eos = get_cabac_terminate( &h->cabac );
2447 if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
2448 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
2451 if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
2452 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
2453 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
2457 if( ++s->mb_x >= s->mb_width ) {
2460 ff_draw_horiz_band(s, 16*s->mb_y, 16);
2462 if(FIELD_OR_MBAFF_PICTURE) {
2464 if(FRAME_MBAFF && s->mb_y < s->mb_height)
2465 predict_field_decoding_flag(h);
2469 if( eos || s->mb_y >= s->mb_height ) {
2470 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
2471 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
2478 int ret = ff_h264_decode_mb_cavlc(h);
2480 if(ret>=0) ff_h264_hl_decode_mb(h);
2482 if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
2484 ret = ff_h264_decode_mb_cavlc(h);
2486 if(ret>=0) ff_h264_hl_decode_mb(h);
2491 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
2492 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
2497 if(++s->mb_x >= s->mb_width){
2500 ff_draw_horiz_band(s, 16*s->mb_y, 16);
2502 if(FIELD_OR_MBAFF_PICTURE) {
2504 if(FRAME_MBAFF && s->mb_y < s->mb_height)
2505 predict_field_decoding_flag(h);
2507 if(s->mb_y >= s->mb_height){
2508 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
2510 if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
2511 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
2515 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
2522 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
2523 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
2524 if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
2525 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
2529 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
2538 for(;s->mb_y < s->mb_height; s->mb_y++){
2539 for(;s->mb_x < s->mb_width; s->mb_x++){
2540 int ret= decode_mb(h);
2542 ff_h264_hl_decode_mb(h);
2545 av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
2546 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
2551 if(++s->mb_x >= s->mb_width){
2553 if(++s->mb_y >= s->mb_height){
2554 if(get_bits_count(s->gb) == s->gb.size_in_bits){
2555 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
2559 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
2566 if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
2567 if(get_bits_count(s->gb) == s->gb.size_in_bits){
2568 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
2572 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
2579 ff_draw_horiz_band(s, 16*s->mb_y, 16);
2582 return -1; //not reached
2586 * Call decode_slice() for each context.
2588 * @param h h264 master context
2589 * @param context_count number of contexts to execute
2591 static void execute_decode_slices(H264Context *h, int context_count){
2592 MpegEncContext * const s = &h->s;
2593 AVCodecContext * const avctx= s->avctx;
2597 if (s->avctx->hwaccel)
2599 if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2601 if(context_count == 1) {
2602 decode_slice(avctx, &h);
2604 for(i = 1; i < context_count; i++) {
2605 hx = h->thread_context[i];
2606 hx->s.error_recognition = avctx->error_recognition;
2607 hx->s.error_count = 0;
2610 avctx->execute(avctx, (void *)decode_slice,
2611 h->thread_context, NULL, context_count, sizeof(void*));
2613 /* pull back stuff from slices to master context */
2614 hx = h->thread_context[context_count - 1];
2615 s->mb_x = hx->s.mb_x;
2616 s->mb_y = hx->s.mb_y;
2617 s->dropable = hx->s.dropable;
2618 s->picture_structure = hx->s.picture_structure;
2619 for(i = 1; i < context_count; i++)
2620 h->s.error_count += h->thread_context[i]->s.error_count;
2625 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
2626 MpegEncContext * const s = &h->s;
2627 AVCodecContext * const avctx= s->avctx;
2629 H264Context *hx; ///< thread context
2630 int context_count = 0;
2631 int next_avc= h->is_avc ? 0 : buf_size;
2633 h->max_contexts = avctx->thread_count;
2636 for(i=0; i<50; i++){
2637 av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
2640 if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
2641 h->current_slice = 0;
2642 if (!s->first_field)
2643 s->current_picture_ptr= NULL;
2644 ff_h264_reset_sei(h);
2655 if(buf_index >= next_avc) {
2656 if(buf_index >= buf_size) break;
2658 for(i = 0; i < h->nal_length_size; i++)
2659 nalsize = (nalsize << 8) | buf[buf_index++];
2660 if(nalsize <= 0 || nalsize > buf_size - buf_index){
2661 av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
2664 next_avc= buf_index + nalsize;
2666 // start code prefix search
2667 for(; buf_index + 3 < next_avc; buf_index++){
2668 // This should always succeed in the first iteration.
2669 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
2673 if(buf_index+3 >= buf_size) break;
2676 if(buf_index >= next_avc) continue;
2679 hx = h->thread_context[context_count];
2681 ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
2682 if (ptr==NULL || dst_length < 0){
2685 i= buf_index + consumed;
2686 if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
2687 buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
2688 s->workaround_bugs |= FF_BUG_TRUNCATED;
2690 if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
2691 while(ptr[dst_length - 1] == 0 && dst_length > 0)
2694 bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
2696 if(s->avctx->debug&FF_DEBUG_STARTCODE){
2697 av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", hx->nal_unit_type, buf_index, buf_size, dst_length);
2700 if (h->is_avc && (nalsize != consumed) && nalsize){
2701 av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
2704 buf_index += consumed;
2706 if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
2707 ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
2712 switch(hx->nal_unit_type){
2714 if (h->nal_unit_type != NAL_IDR_SLICE) {
2715 av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
2718 idr(h); //FIXME ensure we don't loose some frames if there is reordering
2720 init_get_bits(&hx->s.gb, ptr, bit_length);
2722 hx->inter_gb_ptr= &hx->s.gb;
2723 hx->s.data_partitioning = 0;
2725 if((err = decode_slice_header(hx, h)))
2728 if (h->current_slice == 1) {
2729 if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
2731 if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2732 ff_vdpau_h264_picture_start(s);
2735 s->current_picture_ptr->key_frame |=
2736 (hx->nal_unit_type == NAL_IDR_SLICE) ||
2737 (h->sei_recovery_frame_cnt >= 0);
2738 if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
2739 && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
2740 && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
2741 && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
2742 && avctx->skip_frame < AVDISCARD_ALL){
2743 if(avctx->hwaccel) {
2744 if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
2747 if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
2748 static const uint8_t start_code[] = {0x00, 0x00, 0x01};
2749 ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
2750 ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
2756 init_get_bits(&hx->s.gb, ptr, bit_length);
2758 hx->inter_gb_ptr= NULL;
2760 if ((err = decode_slice_header(hx, h)) < 0)
2763 hx->s.data_partitioning = 1;
2767 init_get_bits(&hx->intra_gb, ptr, bit_length);
2768 hx->intra_gb_ptr= &hx->intra_gb;
2771 init_get_bits(&hx->inter_gb, ptr, bit_length);
2772 hx->inter_gb_ptr= &hx->inter_gb;
2774 if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
2775 && s->context_initialized
2777 && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
2778 && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
2779 && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
2780 && avctx->skip_frame < AVDISCARD_ALL)
2784 init_get_bits(&s->gb, ptr, bit_length);
2785 ff_h264_decode_sei(h);
2788 init_get_bits(&s->gb, ptr, bit_length);
2789 ff_h264_decode_seq_parameter_set(h);
2791 if(s->flags& CODEC_FLAG_LOW_DELAY)
2794 if(avctx->has_b_frames < 2)
2795 avctx->has_b_frames= !s->low_delay;
2798 init_get_bits(&s->gb, ptr, bit_length);
2800 ff_h264_decode_picture_parameter_set(h, bit_length);
2804 case NAL_END_SEQUENCE:
2805 case NAL_END_STREAM:
2806 case NAL_FILLER_DATA:
2808 case NAL_AUXILIARY_SLICE:
2811 av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
2814 if(context_count == h->max_contexts) {
2815 execute_decode_slices(h, context_count);
2820 av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
2822 /* Slice could not be decoded in parallel mode, copy down
2823 * NAL unit stuff to context 0 and restart. Note that
2824 * rbsp_buffer is not transferred, but since we no longer
2825 * run in parallel mode this should not be an issue. */
2826 h->nal_unit_type = hx->nal_unit_type;
2827 h->nal_ref_idc = hx->nal_ref_idc;
2833 execute_decode_slices(h, context_count);
2838 * returns the number of bytes consumed for building the current frame
2840 static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
2841 if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
2842 if(pos+10>buf_size) pos=buf_size; // oops ;)
2847 static int decode_frame(AVCodecContext *avctx,
2848 void *data, int *data_size,
2851 const uint8_t *buf = avpkt->data;
2852 int buf_size = avpkt->size;
2853 H264Context *h = avctx->priv_data;
2854 MpegEncContext *s = &h->s;
2855 AVFrame *pict = data;
2858 s->flags= avctx->flags;
2859 s->flags2= avctx->flags2;
2861 /* end of stream, output what is still in the buffers */
2863 if (buf_size == 0) {
2867 //FIXME factorize this with the output code below
2868 out = h->delayed_pic[0];
2870 for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
2871 if(h->delayed_pic[i]->poc < out->poc){
2872 out = h->delayed_pic[i];
2876 for(i=out_idx; h->delayed_pic[i]; i++)
2877 h->delayed_pic[i] = h->delayed_pic[i+1];
2880 *data_size = sizeof(AVFrame);
2881 *pict= *(AVFrame*)out;
2887 buf_index=decode_nal_units(h, buf, buf_size);
2891 if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
2896 if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
2897 if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
2898 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
2902 if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
2903 Picture *out = s->current_picture_ptr;
2904 Picture *cur = s->current_picture_ptr;
2905 int i, pics, out_of_order, out_idx;
2909 if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
2910 /* Wait for second field. */
2914 cur->interlaced_frame = 0;
2915 cur->repeat_pict = 0;
2917 /* Signal interlacing information externally. */
2918 /* Prioritize picture timing SEI information over used decoding process if it exists. */
2920 if(h->sps.pic_struct_present_flag){
2921 switch (h->sei_pic_struct)
2923 case SEI_PIC_STRUCT_FRAME:
2925 case SEI_PIC_STRUCT_TOP_FIELD:
2926 case SEI_PIC_STRUCT_BOTTOM_FIELD:
2927 cur->interlaced_frame = 1;
2929 case SEI_PIC_STRUCT_TOP_BOTTOM:
2930 case SEI_PIC_STRUCT_BOTTOM_TOP:
2931 if (FIELD_OR_MBAFF_PICTURE)
2932 cur->interlaced_frame = 1;
2934 // try to flag soft telecine progressive
2935 cur->interlaced_frame = h->prev_interlaced_frame;
2937 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
2938 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
2939 // Signal the possibility of telecined film externally (pic_struct 5,6)
2940 // From these hints, let the applications decide if they apply deinterlacing.
2941 cur->repeat_pict = 1;
2943 case SEI_PIC_STRUCT_FRAME_DOUBLING:
2944 // Force progressive here, as doubling interlaced frame is a bad idea.
2945 cur->repeat_pict = 2;
2947 case SEI_PIC_STRUCT_FRAME_TRIPLING:
2948 cur->repeat_pict = 4;
2952 if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
2953 cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
2955 /* Derive interlacing flag from used decoding process. */
2956 cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
2958 h->prev_interlaced_frame = cur->interlaced_frame;
2960 if (cur->field_poc[0] != cur->field_poc[1]){
2961 /* Derive top_field_first from field pocs. */
2962 cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
2964 if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
2965 /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
2966 if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
2967 || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
2968 cur->top_field_first = 1;
2970 cur->top_field_first = 0;
2972 /* Most likely progressive */
2973 cur->top_field_first = 0;
2977 //FIXME do something with unavailable reference frames
2979 /* Sort B-frames into display order */
2981 if(h->sps.bitstream_restriction_flag
2982 && s->avctx->has_b_frames < h->sps.num_reorder_frames){
2983 s->avctx->has_b_frames = h->sps.num_reorder_frames;
2987 if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
2988 && !h->sps.bitstream_restriction_flag){
2989 s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
2994 while(h->delayed_pic[pics]) pics++;
2996 assert(pics <= MAX_DELAYED_PIC_COUNT);
2998 h->delayed_pic[pics++] = cur;
2999 if(cur->reference == 0)
3000 cur->reference = DELAYED_PIC_REF;
3002 out = h->delayed_pic[0];
3004 for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
3005 if(h->delayed_pic[i]->poc < out->poc){
3006 out = h->delayed_pic[i];
3009 if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
3010 h->outputed_poc= INT_MIN;
3011 out_of_order = out->poc < h->outputed_poc;
3013 if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
3015 else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
3017 ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
3018 || cur->pict_type == FF_B_TYPE)))
3021 s->avctx->has_b_frames++;
3024 if(out_of_order || pics > s->avctx->has_b_frames){
3025 out->reference &= ~DELAYED_PIC_REF;
3026 for(i=out_idx; h->delayed_pic[i]; i++)
3027 h->delayed_pic[i] = h->delayed_pic[i+1];
3029 if(!out_of_order && pics > s->avctx->has_b_frames){
3030 *data_size = sizeof(AVFrame);
3032 if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
3033 h->outputed_poc = INT_MIN;
3035 h->outputed_poc = out->poc;
3036 *pict= *(AVFrame*)out;
3038 av_log(avctx, AV_LOG_DEBUG, "no picture\n");
3043 assert(pict->data[0] || !*data_size);
3044 ff_print_debug_info(s, pict);
3045 //printf("out %d\n", (int)pict->data[0]);
3047 return get_consumed_bytes(s, buf_index, buf_size);
3050 static inline void fill_mb_avail(H264Context *h){
3051 MpegEncContext * const s = &h->s;
3052 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
3055 h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
3056 h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
3057 h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
3063 h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
3064 h->mb_avail[4]= 1; //FIXME move out
3065 h->mb_avail[5]= 0; //FIXME move out
3073 #define SIZE (COUNT*40)
3079 // int int_temp[10000];
3081 AVCodecContext avctx;
3083 dsputil_init(&dsp, &avctx);
3085 init_put_bits(&pb, temp, SIZE);
3086 printf("testing unsigned exp golomb\n");
3087 for(i=0; i<COUNT; i++){
3089 set_ue_golomb(&pb, i);
3090 STOP_TIMER("set_ue_golomb");
3092 flush_put_bits(&pb);
3094 init_get_bits(&gb, temp, 8*SIZE);
3095 for(i=0; i<COUNT; i++){
3098 s= show_bits(&gb, 24);
3101 j= get_ue_golomb(&gb);
3103 printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
3106 STOP_TIMER("get_ue_golomb");
3110 init_put_bits(&pb, temp, SIZE);
3111 printf("testing signed exp golomb\n");
3112 for(i=0; i<COUNT; i++){
3114 set_se_golomb(&pb, i - COUNT/2);
3115 STOP_TIMER("set_se_golomb");
3117 flush_put_bits(&pb);
3119 init_get_bits(&gb, temp, 8*SIZE);
3120 for(i=0; i<COUNT; i++){
3123 s= show_bits(&gb, 24);
3126 j= get_se_golomb(&gb);
3127 if(j != i - COUNT/2){
3128 printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
3131 STOP_TIMER("get_se_golomb");
3135 printf("testing 4x4 (I)DCT\n");
3138 uint8_t src[16], ref[16];
3139 uint64_t error= 0, max_error=0;
3141 for(i=0; i<COUNT; i++){
3143 // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
3144 for(j=0; j<16; j++){
3145 ref[j]= random()%255;
3146 src[j]= random()%255;
3149 h264_diff_dct_c(block, src, ref, 4);
3152 for(j=0; j<16; j++){
3153 // printf("%d ", block[j]);
3154 block[j]= block[j]*4;
3155 if(j&1) block[j]= (block[j]*4 + 2)/5;
3156 if(j&4) block[j]= (block[j]*4 + 2)/5;
3160 h->h264dsp.h264_idct_add(ref, block, 4);
3161 /* for(j=0; j<16; j++){
3162 printf("%d ", ref[j]);
3166 for(j=0; j<16; j++){
3167 int diff= FFABS(src[j] - ref[j]);
3170 max_error= FFMAX(max_error, diff);
3173 printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
3174 printf("testing quantizer\n");
3175 for(qp=0; qp<52; qp++){
3177 src1_block[i]= src2_block[i]= random()%255;
3180 printf("Testing NAL layer\n");
3182 uint8_t bitstream[COUNT];
3183 uint8_t nal[COUNT*2];
3185 memset(&h, 0, sizeof(H264Context));
3187 for(i=0; i<COUNT; i++){
3195 for(j=0; j<COUNT; j++){
3196 bitstream[j]= (random() % 255) + 1;
3199 for(j=0; j<zeros; j++){
3200 int pos= random() % COUNT;
3201 while(bitstream[pos] == 0){
3210 nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
3212 printf("encoding failed\n");
3216 out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
3220 if(out_length != COUNT){
3221 printf("incorrect length %d %d\n", out_length, COUNT);
3225 if(consumed != nal_length){
3226 printf("incorrect consumed length %d %d\n", nal_length, consumed);
3230 if(memcmp(bitstream, out, COUNT)){
3231 printf("mismatch\n");
3237 printf("Testing RBSP\n");
3245 av_cold void ff_h264_free_context(H264Context *h)
3249 free_tables(h); //FIXME cleanup init stuff perhaps
3251 for(i = 0; i < MAX_SPS_COUNT; i++)
3252 av_freep(h->sps_buffers + i);
3254 for(i = 0; i < MAX_PPS_COUNT; i++)
3255 av_freep(h->pps_buffers + i);
3258 av_cold int ff_h264_decode_end(AVCodecContext *avctx)
3260 H264Context *h = avctx->priv_data;
3261 MpegEncContext *s = &h->s;
3263 ff_h264_free_context(h);
3267 // memset(h, 0, sizeof(H264Context));
3273 AVCodec h264_decoder = {
3277 sizeof(H264Context),
3278 ff_h264_decode_init,
3282 /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3284 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
3287 #if CONFIG_H264_VDPAU_DECODER
3288 AVCodec h264_vdpau_decoder = {
3292 sizeof(H264Context),
3293 ff_h264_decode_init,
3297 CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3299 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
3300 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},