# HG changeset patch # User benoit # Date 1180449329 0 # Node ID 4ff805f873916920ca84864901483c7d70d5a332 # Parent 4f36b52179d168805638295199a9c6ec05c95c18 allocate PPS and SPS dynamically patch by Andreas ªÓman andreas ta olebyn tod nu reference thread: subject: [FFmpeg-devel] [PATCH] h264: allocate PPS and SPS dynamically date: 05/28/2007 03:00 PM diff -r 4f36b52179d1 -r 4ff805f87391 h264.c --- a/h264.c Tue May 29 12:20:40 2007 +0000 +++ b/h264.c Tue May 29 14:35:29 2007 +0000 @@ -2766,6 +2766,7 @@ } static void free_tables(H264Context *h){ + int i; av_freep(&h->intra4x4_pred_mode); av_freep(&h->chroma_pred_mode_table); av_freep(&h->cbp_table); @@ -2782,6 +2783,12 @@ av_freep(&h->mb2b8_xy); av_freep(&h->s.obmc_scratchpad); + + for(i = 0; i < MAX_SPS_COUNT; i++) + av_freep(h->sps_buffers + i); + + for(i = 0; i < MAX_PPS_COUNT; i++) + av_freep(h->pps_buffers + i); } static void init_dequant8_coeff_table(H264Context *h){ @@ -4194,17 +4201,17 @@ av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n"); return -1; } - h->pps= h->pps_buffer[pps_id]; - if(h->pps.slice_group_count == 0){ + if(!h->pps_buffers[pps_id]) { av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n"); return -1; } - - h->sps= h->sps_buffer[ h->pps.sps_id ]; - if(h->sps.log2_max_frame_num == 0){ + h->pps= *h->pps_buffers[pps_id]; + + if(!h->sps_buffers[h->pps.sps_id]) { av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n"); return -1; } + h->sps = *h->sps_buffers[h->pps.sps_id]; if(h->dequant_coeff_pps != pps_id){ h->dequant_coeff_pps = pps_id; @@ -7399,6 +7406,26 @@ } } +/** + * Returns and optionally allocates SPS / PPS structures in the supplied array 'vec' + */ +static void * +alloc_parameter_set(H264Context *h, void **vec, const unsigned int id, const unsigned int max, + const size_t size, const char *name) +{ + if(id>=max) { + av_log(h->s.avctx, AV_LOG_ERROR, "%s_id (%d) out of range\n", name, id); + return NULL; + } + + if(!vec[id]) { + vec[id] = av_mallocz(size); + if(vec[id] == NULL) + av_log(h->s.avctx, AV_LOG_ERROR, "cannot allocate memory for %s\n", name); + } + return vec[id]; +} + static inline int decode_seq_parameter_set(H264Context *h){ MpegEncContext * const s = &h->s; int profile_idc, level_idc; @@ -7415,13 +7442,10 @@ level_idc= get_bits(&s->gb, 8); sps_id= get_ue_golomb(&s->gb); - if (sps_id >= MAX_SPS_COUNT){ - // ok it has gone out of hand, someone is sending us bad stuff. - av_log(h->s.avctx, AV_LOG_ERROR, "illegal sps_id (%d)\n", sps_id); + sps = alloc_parameter_set(h, (void **)h->sps_buffers, sps_id, MAX_SPS_COUNT, sizeof(SPS), "sps"); + if(sps == NULL) return -1; - } - - sps= &h->sps_buffer[ sps_id ]; + sps->profile_idc= profile_idc; sps->level_idc= level_idc; @@ -7531,14 +7555,12 @@ unsigned int tmp, pps_id= get_ue_golomb(&s->gb); PPS *pps; - if(pps_id>=MAX_PPS_COUNT){ - av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n"); + pps = alloc_parameter_set(h, (void **)h->pps_buffers, pps_id, MAX_PPS_COUNT, sizeof(PPS), "pps"); + if(pps == NULL) return -1; - } - pps = &h->pps_buffer[pps_id]; tmp= get_ue_golomb(&s->gb); - if(tmp>=MAX_SPS_COUNT){ + if(tmp>=MAX_SPS_COUNT || h->sps_buffers[tmp] == NULL){ av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n"); return -1; } @@ -7608,7 +7630,7 @@ if(get_bits_count(&s->gb) < bit_length){ pps->transform_8x8_mode= get_bits1(&s->gb); - decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8); + decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8); get_se_golomb(&s->gb); //second_chroma_qp_index_offset } diff -r 4f36b52179d1 -r 4ff805f87391 h264.h --- a/h264.h Tue May 29 12:20:40 2007 +0000 +++ b/h264.h Tue May 29 14:35:29 2007 +0000 @@ -243,10 +243,10 @@ int unknown_svq3_flag; int next_slice_index; - SPS sps_buffer[MAX_SPS_COUNT]; + SPS *sps_buffers[MAX_SPS_COUNT]; SPS sps; ///< current sps - PPS pps_buffer[MAX_PPS_COUNT]; + PPS *pps_buffers[MAX_PPS_COUNT]; /** * current pps */