Skip to content
Permalink
Browse files

WIP fix conflict

Main changes:

- a local GetBitContext is created for the various
  ff_h264_decode_seq_parameter_set() attempts

- just like the old code, remove_sps() is adjusted so it doesn't remove
  the pps.

  Fixes decode with Ticket #631
  http://ffmpeg.org/pipermail/ffmpeg-user/attachments/20111108/dae58f17/attachment.mp4

  TODO: It still requires -flags2 +chunks though, which wasn't needed
  previously.

- keep {sps,pps}_ref pointers that stay alive even when the active
  pps/sps get removed from the available lists (patch by michaelni with
  additionnal frees in ff_h264_free_context() from mateo)

- added a check on sps in avpriv_h264_has_num_reorder_frames() to fix
  crashes with mpegts_with_dvbsubs.ts from Ticket #4074
  http://samples.ffmpeg.org/ffmpeg-bugs/trac/ticket4074/mpegts_with_dvbsubs.ts

- in h264_parser.c:h264_parse(), after the ff_h264_decode_extradata() is
  called, the pps and sps from the local parser context are updated with
  the pps and sps from the used h264context. This fixes fate-flv-demux.

TODO: fixes #631 so it works without -flags2 +chunk

TODO: make fate-h264-xavc-4389 THREADS=4 fails
      apparently due to "h->dequant_coeff_pps != pps_id" becoming
      "h->ps.pps != pps"
      NOTE: this kind of checks can be found in several other places
      (git grep 'h->ps.[sp]ps [^ ]=')

TODO: http://samples.ffmpeg.org/MPEG-VOB/transport/issue2.ts
      the following data changes:
      ./ffmpeg -fdebug +ts -i issue2.ts -vframes 12 -f null - 2>&1 | egrep 'read_frame_internal stream=' | sed 's/@.*]//'
      also some flags and dts are lost.

TODO: squash in merge commit
  • Loading branch information
ubitux committed Jun 4, 2016
1 parent d233784 commit 5cb25f94ffb5e096bac6156097c93cdba5c95078
@@ -103,13 +103,8 @@ static void fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *
pp->wBitFields = ((h->picture_structure != PICT_FRAME) << 0) |
((sps->mb_aff &&
(h->picture_structure == PICT_FRAME)) << 1) |
<<<<<<< HEAD
(h->sps.residual_color_transform_flag << 2) |
/* sp_for_switch_flag (not implemented by FFmpeg) */
=======
(sps->residual_color_transform_flag << 2) |
/* sp_for_switch_flag (not implemented by Libav) */
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
/* sp_for_switch_flag (not implemented by FFmpeg) */
(0 << 3) |
(sps->chroma_format_idc << 4) |
((h->nal_ref_idc != 0) << 6) |
@@ -171,6 +166,7 @@ static void fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *

static void fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, const H264Context *h, DXVA_Qmatrix_H264 *qm)
{
const PPS *pps = h->ps.pps;
unsigned i, j;
memset(qm, 0, sizeof(*qm));
if (DXVA_CONTEXT_WORKAROUND(avctx, ctx) & FF_DXVA2_WORKAROUND_SCALING_LIST_ZIGZAG) {
@@ -288,13 +284,8 @@ static void fill_slice_long(AVCodecContext *avctx, DXVA_Slice_H264_Long *slice,
}
}
}
<<<<<<< HEAD
slice->slice_qs_delta = 0; /* XXX not implemented by FFmpeg */
slice->slice_qp_delta = sl->qscale - h->pps.init_qp;
=======
slice->slice_qs_delta = 0; /* XXX not implemented by Libav */
slice->slice_qp_delta = sl->qscale - h->ps.pps->init_qp;
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
slice->redundant_pic_cnt = sl->redundant_pic_count;
if (sl->slice_type == AV_PICTURE_TYPE_B)
slice->direct_spatial_mv_pred_flag = sl->direct_spatial_mv_pred;
@@ -60,7 +60,7 @@ const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
{
H264Context *h = avctx->priv_data;
return h ? h->sps.num_reorder_frames : 0;
return h && h->ps.sps ? h->ps.sps->num_reorder_frames : 0;
}

static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
@@ -419,15 +419,11 @@ static int h264_init_context(AVCodecContext *avctx, H264Context *h)
int i;

h->avctx = avctx;
<<<<<<< HEAD
h->backup_width = -1;
h->backup_height = -1;
h->backup_pix_fmt = AV_PIX_FMT_NONE;
h->dequant_coeff_pps = -1;
h->current_sps_id = -1;
h->cur_chroma_format_idc = -1;
=======
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d

h->picture_structure = PICT_FRAME;
h->slice_context_count = 1;
@@ -641,11 +637,7 @@ static void decode_postinit(H264Context *h, int setup_finished)
/* Derive top_field_first from field pocs. */
cur->f->top_field_first = cur->field_poc[0] < cur->field_poc[1];
} else {
<<<<<<< HEAD
if (h->sps.pic_struct_present_flag) {
=======
if (cur->f->interlaced_frame || sps->pic_struct_present_flag) {
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
if (sps->pic_struct_present_flag) {
/* Use picture timing SEI information. Even if it is a
* information of a past frame, better than nothing. */
if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
@@ -742,15 +734,9 @@ static void decode_postinit(H264Context *h, int setup_finished)
// FIXME do something with unavailable reference frames

/* Sort B-frames into display order */
<<<<<<< HEAD
if (h->sps.bitstream_restriction_flag ||
h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, h->sps.num_reorder_frames);
=======
if (sps->bitstream_restriction_flag ||
h->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
}
h->low_delay = !h->avctx->has_b_frames;

@@ -773,7 +759,7 @@ static void decode_postinit(H264Context *h, int setup_finished)
h->last_pocs[i] = INT_MIN;
h->last_pocs[0] = cur->poc;
cur->mmco_reset = 1;
} else if(h->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
} else if(h->avctx->has_b_frames < out_of_order && !sps->bitstream_restriction_flag){
av_log(h->avctx, AV_LOG_INFO, "Increasing reorder buffer to %d\n", out_of_order);
h->avctx->has_b_frames = out_of_order;
h->low_delay = 0;
@@ -802,28 +788,7 @@ static void decode_postinit(H264Context *h, int setup_finished)
if (h->avctx->has_b_frames == 0 &&
(h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset))
h->next_outputed_poc = INT_MIN;
<<<<<<< HEAD
out_of_order = out->poc < h->next_outputed_poc;
=======
out_of_order = !out->f->key_frame && !h->mmco_reset &&
(out->poc < h->next_outputed_poc);

if (sps->bitstream_restriction_flag &&
h->avctx->has_b_frames >= sps->num_reorder_frames) {
} else if (out_of_order && pics - 1 == h->avctx->has_b_frames &&
h->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, cnt);
}
h->low_delay = 0;
} else if (h->low_delay &&
((h->next_outputed_poc != INT_MIN &&
out->poc > h->next_outputed_poc + 2) ||
cur->f->pict_type == AV_PICTURE_TYPE_B)) {
h->low_delay = 0;
h->avctx->has_b_frames++;
}
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d

if (out_of_order || pics > h->avctx->has_b_frames) {
out->reference &= ~DELAYED_PIC_REF;
@@ -1182,23 +1147,17 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
if ((err = ff_h264_decode_slice_header(h, sl)))
break;

<<<<<<< HEAD
if (h->sei_recovery_frame_cnt >= 0) {
if (h->frame_num != h->sei_recovery_frame_cnt || sl->slice_type_nos != AV_PICTURE_TYPE_I)
h->valid_recovery_point = 1;

if ( h->recovery_frame < 0
|| av_mod_uintp2(h->recovery_frame - h->frame_num, h->sps.log2_max_frame_num) > h->sei_recovery_frame_cnt) {
h->recovery_frame = av_mod_uintp2(h->frame_num + h->sei_recovery_frame_cnt, h->sps.log2_max_frame_num);
|| av_mod_uintp2(h->recovery_frame - h->frame_num, h->ps.sps->log2_max_frame_num) > h->sei_recovery_frame_cnt) {
h->recovery_frame = av_mod_uintp2(h->frame_num + h->sei_recovery_frame_cnt, h->ps.sps->log2_max_frame_num);

if (!h->valid_recovery_point)
h->recovery_frame = h->frame_num;
}
=======
if (h->sei_recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) &
((1 << h->ps.sps->log2_max_frame_num) - 1);
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
}

h->cur_pic_ptr->f->key_frame |= (nal->type == NAL_IDR_SLICE);
@@ -1264,25 +1223,18 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
goto end;
break;
case NAL_SPS:
<<<<<<< HEAD
h->gb = nal->gb;
if (ff_h264_decode_seq_parameter_set(h, 0) >= 0)
case NAL_SPS: {
GetBitContext tmp_gb = nal->gb;
if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
break;
av_log(h->avctx, AV_LOG_DEBUG,
"SPS decoding failure, trying again with the complete NAL\n");
init_get_bits8(&h->gb, nal->raw_data + 1, nal->raw_size - 1);
if (ff_h264_decode_seq_parameter_set(h, 0) >= 0)
init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1);
if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
break;
h->gb = nal->gb;
ff_h264_decode_seq_parameter_set(h, 1);

=======
ret = ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps);
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
goto end;
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps, 1);
break;
}
case NAL_PPS:
ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
nal->size_bits);
@@ -1350,7 +1302,10 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
* past end by one (callers fault) and resync_mb_y != 0
* causes problems for the first MB line, too.
*/
if (!FIELD_PICTURE(h) && h->current_slice && !h->sps.new && h->enable_er) {
if (!FIELD_PICTURE(h) && h->current_slice &&
h->ps.sps == (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data &&
h->enable_er) {

H264SliceContext *sl = h->slice_ctx;
int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];

@@ -1408,7 +1363,6 @@ static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
if (ret < 0)
return ret;

<<<<<<< HEAD
av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(h), 0);

h->backup_width = h->avctx->width;
@@ -1429,16 +1383,6 @@ static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
int vshift = (i > 0) ? desc->log2_chroma_h : 0;
int off = ((srcp->crop_left >> hshift) << h->pixel_shift) +
(srcp->crop_top >> vshift) * dst->linesize[i];
=======
if (!h->ps.sps || !h->ps.sps->crop)
return 0;

for (i = 0; i < 3; i++) {
int hshift = (i > 0) ? h->chroma_x_shift : 0;
int vshift = (i > 0) ? h->chroma_y_shift : 0;
int off = ((h->ps.sps->crop_left >> hshift) << h->pixel_shift) +
(h->ps.sps->crop_top >> vshift) * dst->linesize[i];
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
dst->data[i] += off;
}
return 0;
@@ -1646,6 +1590,9 @@ av_cold void ff_h264_free_context(H264Context *h)
for (i = 0; i < MAX_PPS_COUNT; i++)
av_buffer_unref(&h->ps.pps_list[i]);

av_buffer_unref(&h->ps.sps_ref);
av_buffer_unref(&h->ps.pps_ref);

ff_h2645_packet_uninit(&h->pkt);
}

@@ -95,20 +95,12 @@
#define FIELD_OR_MBAFF_PICTURE(h) (FRAME_MBAFF(h) || FIELD_PICTURE(h))

#ifndef CABAC
<<<<<<< HEAD
#define CABAC(h) (h)->pps.cabac
#define CABAC(h) (h)->ps.pps->cabac
#endif

#define CHROMA(h) ((h)->sps.chroma_format_idc)
#define CHROMA422(h) ((h)->sps.chroma_format_idc == 2)
#define CHROMA444(h) ((h)->sps.chroma_format_idc == 3)
=======
#define CABAC(h) h->ps.pps->cabac
#endif

#define CHROMA422(h) (h->ps.sps->chroma_format_idc == 2)
#define CHROMA444(h) (h->ps.sps->chroma_format_idc == 3)
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
#define CHROMA(h) ((h)->ps.sps->chroma_format_idc)
#define CHROMA422(h) ((h)->ps.sps->chroma_format_idc == 2)
#define CHROMA444(h) ((h)->ps.sps->chroma_format_idc == 3)

#define EXTENDED_SAR 255

@@ -240,12 +232,8 @@ typedef struct SPS {
int bit_depth_chroma; ///< bit_depth_chroma_minus8 + 8
int residual_color_transform_flag; ///< residual_colour_transform_flag
int constraint_set_flags; ///< constraint_set[0-3]_flag
<<<<<<< HEAD
int new; ///< flag to keep track if the decoder context needs re-init due to changed SPS
uint8_t data[4096];
size_t data_size;
=======
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
} SPS;

/**
@@ -271,22 +259,21 @@ typedef struct PPS {
uint8_t scaling_matrix8[6][64];
uint8_t chroma_qp_table[2][QP_MAX_NUM+1]; ///< pre-scaled (with chroma_qp_index_offset) version of qp_table
int chroma_qp_diff;
<<<<<<< HEAD
uint8_t data[4096];
size_t data_size;
=======

uint32_t dequant4_buffer[6][QP_MAX_NUM + 1][16];
uint32_t dequant8_buffer[6][QP_MAX_NUM + 1][64];
uint32_t(*dequant4_coeff[6])[16];
uint32_t(*dequant8_coeff[6])[64];
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
} PPS;

typedef struct H264ParamSets {
AVBufferRef *sps_list[MAX_SPS_COUNT];
AVBufferRef *pps_list[MAX_PPS_COUNT];

AVBufferRef *pps_ref;
AVBufferRef *sps_ref;
/* currently active parameters sets */
const PPS *pps;
// FIXME this should properly be const
@@ -600,21 +587,11 @@ typedef struct H264Context {
uint32_t *mb2br_xy;
int b_stride; // FIXME use s->b4_stride

<<<<<<< HEAD

unsigned current_sps_id; ///< id of the current SPS
SPS sps; ///< current sps
PPS pps; ///< current pps

int au_pps_id; ///< pps_id of current access unit

uint32_t dequant4_buffer[6][QP_MAX_NUM + 1][16]; // FIXME should these be moved down?
uint32_t dequant8_buffer[6][QP_MAX_NUM + 1][64];
uint32_t(*dequant4_coeff[6])[16];
uint32_t(*dequant8_coeff[6])[64];

=======
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
uint16_t *slice_table; ///< slice_table_base + 2*mb_stride + 1

// interlacing specific flags
@@ -878,12 +855,8 @@ int ff_h264_decode_sei(H264Context *h);
/**
* Decode SPS
*/
<<<<<<< HEAD
int ff_h264_decode_seq_parameter_set(H264Context *h, int ignore_truncation);
=======
int ff_h264_decode_seq_parameter_set(GetBitContext *gb, AVCodecContext *avctx,
H264ParamSets *ps);
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
H264ParamSets *ps, int ignore_truncation);

/**
* compute profile from sps
@@ -946,7 +919,7 @@ int ff_h264_decode_mb_cabac(const H264Context *h, H264SliceContext *sl);

void ff_h264_init_cabac_states(const H264Context *h, H264SliceContext *sl);

void ff_h264_init_dequant_tables(H264Context *h);
void ff_h264_init_dequant_tables(PPS *pps, const SPS *sps);

void ff_h264_direct_dist_scale_factor(const H264Context *const h, H264SliceContext *sl);
void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl);
@@ -1142,7 +1142,7 @@ int ff_h264_decode_mb_cavlc(const H264Context *h, H264SliceContext *sl)
return -1;
}
} else {
const int num_c8x8 = h->sps.chroma_format_idc;
const int num_c8x8 = h->ps.sps->chroma_format_idc;

if(cbp&0x30){
for(chroma_idx=0; chroma_idx<2; chroma_idx++)
@@ -1171,32 +1171,6 @@ int ff_h264_decode_mb_cavlc(const H264Context *h, H264SliceContext *sl)
fill_rectangle(&sl->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);
fill_rectangle(&sl->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);
}
<<<<<<< HEAD
=======
} else /* yuv420 */ {
if(cbp&0x30){
for(chroma_idx=0; chroma_idx<2; chroma_idx++)
if (decode_residual(h, sl, gb, sl->mb + ((256 + 16 * 16 * chroma_idx) << pixel_shift),
CHROMA_DC_BLOCK_INDEX + chroma_idx, ff_h264_chroma_dc_scan, NULL, 4) < 0) {
return -1;
}
}

if(cbp&0x20){
for(chroma_idx=0; chroma_idx<2; chroma_idx++){
const uint32_t *qmul = h->ps.pps->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][sl->chroma_qp[chroma_idx]];
for(i4x4=0; i4x4<4; i4x4++){
const int index= 16 + 16*chroma_idx + i4x4;
if( decode_residual(h, sl, gb, sl->mb + (16*index << pixel_shift), index, scan + 1, qmul, 15) < 0){
return -1;
}
}
}
}else{
fill_rectangle(&sl->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);
fill_rectangle(&sl->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);
}
>>>>>>> 3176217c60ca7828712985092d9102d331ea4f3d
}
}else{
fill_rectangle(&sl->non_zero_count_cache[scan8[ 0]], 4, 4, 8, 0, 1);

0 comments on commit 5cb25f9

Please sign in to comment.