History

avcodec/hevc_ps: don't use a fixed sized buffer for parameter set raw data

Allocate it instead, and use it to compare sets instead of the parsed struct.

James Almer committed 2 years ago

Commit 547c9201 parent 6bf17132 files · +77 −74Browse files

Unified · Split

2 files
  1. libavcodec/hevc_ps.c +72 −68
  2. libavcodec/hevc_ps.h +5 −6
libavcodec/hevc_ps.c+72−68
@@ -442,47 +442,43 @@ static int decode_hrd(GetBitContext *gb, int common_inf_present, · 441 unmodified lines
442442 return 0;
443443 }
444444
445−static void uninit_vps(FFRefStructOpaque opaque, void *obj)
445+static void hevc_vps_free(FFRefStructOpaque opaque, void *obj)
446446 {
447447 HEVCVPS *vps = obj;
448448
449449 av_freep(&vps->hdr);
450−}
451−
452−static int compare_vps(const HEVCVPS *vps1, const HEVCVPS *vps2)
453−{
454− if (!memcmp(vps1, vps2, offsetof(HEVCVPS, hdr)))
455− return !vps1->vps_num_hrd_parameters ||
456− !memcmp(vps1->hdr, vps2->hdr, vps1->vps_num_hrd_parameters * sizeof(*vps1->hdr));
457−
458− return 0;
450+ av_freep(&vps->data);
459451 }
460452
461453 int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
462454 HEVCParamSets *ps)
463455 {
464456 int i,j;
465− int vps_id = 0;
466− ptrdiff_t nal_size;
467− HEVCVPS *vps = ff_refstruct_alloc_ext(sizeof(*vps), 0, NULL, uninit_vps);
457+ int vps_id = get_bits(gb, 4);
458+ ptrdiff_t nal_size = gb->buffer_end - gb->buffer;
459+ int ret = AVERROR_INVALIDDATA;
460+ HEVCVPS *vps;
461+
462+ if (ps->pps_list[vps_id]) {
463+ const HEVCVPS *vps1 = ps->vps_list[vps_id];
464+ if (vps1->data_size == nal_size &&
465+ !memcmp(vps1->data, gb->buffer, vps1->data_size))
466+ return 0;
467+ }
468468
469+ vps = ff_refstruct_alloc_ext(sizeof(*vps), 0, NULL, hevc_vps_free);
469470 if (!vps)
470471 return AVERROR(ENOMEM);
471472
472473 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
473474
474− nal_size = gb->buffer_end - gb->buffer;
475− if (nal_size > sizeof(vps->data)) {
476− av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "
477− "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
478− nal_size, sizeof(vps->data));
479− vps->data_size = sizeof(vps->data);
480− } else {
481− vps->data_size = nal_size;
475+ vps->data_size = nal_size;
476+ vps->data = av_memdup(gb->buffer, nal_size);
477+ if (!vps->data) {
478+ ret = AVERROR(ENOMEM);
479+ goto err;
482480 }
483− memcpy(vps->data, gb->buffer, vps->data_size);
484−
485− vps_id = vps->vps_id = get_bits(gb, 4);
481+ vps->vps_id = vps_id;
486482
487483 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
488484 av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");
@@ -579,19 +575,14 @@ int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, · 90 unmodified lines
579575 goto err;
580576 }
581577
582− if (ps->vps_list[vps_id] &&
583− compare_vps(ps->vps_list[vps_id], vps)) {
584− ff_refstruct_unref(&vps);
585− } else {
586− remove_vps(ps, vps_id);
587− ps->vps_list[vps_id] = vps;
588− }
578+ remove_vps(ps, vps_id);
579+ ps->vps_list[vps_id] = vps;
589580
590581 return 0;
591582
592583 err:
593584 ff_refstruct_unref(&vps);
594− return AVERROR_INVALIDDATA;
585+ return ret;
595586 }
596587
597588 static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,
@@ -1294,37 +1285,43 @@ int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, unsigned int *sps_id, · 696 unmodified lines
12941285 return 0;
12951286 }
12961287
1288+static void hevc_sps_free(FFRefStructOpaque opaque, void *obj)
1289+{
1290+ HEVCSPS *sps = obj;
1291+
1292+ av_freep(&sps->data);
1293+}
1294+
1295+static int compare_sps(const HEVCSPS *sps1, const HEVCSPS *sps2)
1296+{
1297+ return sps1->data_size == sps2->data_size &&
1298+ !memcmp(sps1->data, sps2->data, sps1->data_size);
1299+}
1300+
12971301 int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
12981302 HEVCParamSets *ps, int apply_defdispwin)
12991303 {
1300− HEVCSPS *sps = ff_refstruct_allocz(sizeof(*sps));
1304+ HEVCSPS *sps = ff_refstruct_alloc_ext(sizeof(*sps), 0, NULL, hevc_sps_free);
13011305 unsigned int sps_id;
13021306 int ret;
1303− ptrdiff_t nal_size;
13041307
13051308 if (!sps)
13061309 return AVERROR(ENOMEM);
13071310
13081311 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
13091312
1310− nal_size = gb->buffer_end - gb->buffer;
1311− if (nal_size > sizeof(sps->data)) {
1312− av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized SPS "
1313− "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1314− nal_size, sizeof(sps->data));
1315− sps->data_size = sizeof(sps->data);
1316− } else {
1317− sps->data_size = nal_size;
1313+ sps->data_size = gb->buffer_end - gb->buffer;
1314+ sps->data = av_memdup(gb->buffer, sps->data_size);
1315+ if (!sps->data) {
1316+ ret = AVERROR(ENOMEM);
1317+ goto err;
13181318 }
1319− memcpy(sps->data, gb->buffer, sps->data_size);
13201319
13211320 ret = ff_hevc_parse_sps(sps, gb, &sps_id,
13221321 apply_defdispwin,
13231322 ps->vps_list, avctx);
1324− if (ret < 0) {
1325− ff_refstruct_unref(&sps);
1326− return ret;
1327− }
1323+ if (ret < 0)
1324+ goto err;
13281325
13291326 if (avctx->debug & FF_DEBUG_BITSTREAM) {
13301327 av_log(avctx, AV_LOG_DEBUG,
@@ -1340,7 +1337,7 @@ int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, · 9 unmodified lines
13401337 * original one.
13411338 * otherwise drop all PPSes that depend on it */
13421339 if (ps->sps_list[sps_id] &&
1343− !memcmp(ps->sps_list[sps_id], sps, sizeof(*sps))) {
1340+ compare_sps(ps->sps_list[sps_id], sps)) {
13441341 ff_refstruct_unref(&sps);
13451342 } else {
13461343 remove_sps(ps, sps_id);
@@ -1348,6 +1345,9 @@ int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, · 1 unmodified line
13481345 }
13491346
13501347 return 0;
1348+err:
1349+ ff_refstruct_unref(&sps);
1350+ return ret;
13511351 }
13521352
13531353 static void hevc_pps_free(FFRefStructOpaque unused, void *obj)
@@ -1364,6 +1364,7 @@ static void hevc_pps_free(FFRefStructOpaque unused, void *obj) · 10 unmodified lines
13641364 av_freep(&pps->tile_pos_rs);
13651365 av_freep(&pps->tile_id);
13661366 av_freep(&pps->min_tb_addr_zs_tab);
1367+ av_freep(&pps->data);
13671368 }
13681369
13691370 static void colour_mapping_octants(GetBitContext *gb, HEVCPPS *pps, int inp_depth,
@@ -1762,27 +1763,35 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, · 392 unmodified lines
17621763 const HEVCSPS *sps = NULL;
17631764 const HEVCVPS *vps = NULL;
17641765 int i, ret = 0;
1765− unsigned int pps_id = 0;
1766− ptrdiff_t nal_size;
1766+ ptrdiff_t nal_size = gb->buffer_end - gb->buffer;
1767+ unsigned int pps_id = get_ue_golomb_long(gb);
17671768 unsigned log2_parallel_merge_level_minus2;
1769+ HEVCPPS *pps;
1770+
1771+ av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
17681772
1769− HEVCPPS *pps = ff_refstruct_alloc_ext(sizeof(*pps), 0, NULL, hevc_pps_free);
1773+ if (pps_id >= HEVC_MAX_PPS_COUNT) {
1774+ av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1775+ return AVERROR_INVALIDDATA;
1776+ }
17701777
1778+ if (ps->pps_list[pps_id]) {
1779+ const HEVCPPS *pps1 = ps->pps_list[pps_id];
1780+ if (pps1->data_size == nal_size &&
1781+ !memcmp(pps1->data, gb->buffer, pps1->data_size))
1782+ return 0;
1783+ }
1784+
1785+ pps = ff_refstruct_alloc_ext(sizeof(*pps), 0, NULL, hevc_pps_free);
17711786 if (!pps)
17721787 return AVERROR(ENOMEM);
17731788
1774− av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1775−
1776− nal_size = gb->buffer_end - gb->buffer;
1777− if (nal_size > sizeof(pps->data)) {
1778− av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "
1779− "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",
1780− nal_size, sizeof(pps->data));
1781− pps->data_size = sizeof(pps->data);
1782− } else {
1783− pps->data_size = nal_size;
1789+ pps->data_size = nal_size;
1790+ pps->data = av_memdup(gb->buffer, nal_size);
1791+ if (!pps->data) {
1792+ ret = AVERROR_INVALIDDATA;
1793+ goto err;
17841794 }
1785− memcpy(pps->data, gb->buffer, pps->data_size);
17861795
17871796 // Default values
17881797 pps->loop_filter_across_tiles_enabled_flag = 1;
@@ -1795,12 +1804,7 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, · 6 unmodified lines
17951804 pps->log2_max_transform_skip_block_size = 2;
17961805
17971806 // Coded parameters
1798− pps_id = pps->pps_id = get_ue_golomb_long(gb);
1799− if (pps_id >= HEVC_MAX_PPS_COUNT) {
1800− av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
1801− ret = AVERROR_INVALIDDATA;
1802− goto err;
1803− }
1807+ pps->pps_id = pps_id;
18041808 pps->sps_id = get_ue_golomb_long(gb);
18051809 if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
18061810 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);
libavcodec/hevc_ps.h+5−6
@@ -172,11 +172,10 @@ typedef struct HEVCVPS { · 171 unmodified lines
172172 int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
173173 int vps_num_hrd_parameters;
174174
175− uint8_t data[4096];
176− int data_size;
177− /* Put this at the end of the structure to make it easier to calculate the
178− * size before this pointer, which is used for memcmp */
179175 HEVCHdrParams *hdr;
176+
177+ uint8_t *data;
178+ int data_size;
180179 } HEVCVPS;
181180
182181 typedef struct ScalingList {
@@ -299,7 +298,7 @@ typedef struct HEVCSPS { · 116 unmodified lines
299298
300299 int qp_bd_offset;
301300
302− uint8_t data[4096];
301+ uint8_t *data;
303302 int data_size;
304303 } HEVCSPS;
305304
@@ -434,7 +433,7 @@ typedef struct HEVCPPS { · 128 unmodified lines
434433 int *min_tb_addr_zs; ///< MinTbAddrZS
435434 int *min_tb_addr_zs_tab;///< MinTbAddrZS
436435
437− uint8_t data[4096];
436+ uint8_t *data;
438437 int data_size;
439438 } HEVCPPS;
440439

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t