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
442 return 0;442 return 0;
443}443}
444 444
445static void uninit_vps(FFRefStructOpaque opaque, void *obj)445static void hevc_vps_free(FFRefStructOpaque opaque, void *obj)
446{446{
447 HEVCVPS *vps = obj;447 HEVCVPS *vps = obj;
448 448
449 av_freep(&vps->hdr);449 av_freep(&vps->hdr);
450}450 av_freep(&vps->data);
451
452static 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;
459}451}
460 452
461int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,453int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
462 HEVCParamSets *ps)454 HEVCParamSets *ps)
463{455{
464 int i,j;456 int i,j;
465 int vps_id = 0;457 int vps_id = get_bits(gb, 4);
466 ptrdiff_t nal_size;458 ptrdiff_t nal_size = gb->buffer_end - gb->buffer;
467 HEVCVPS *vps = ff_refstruct_alloc_ext(sizeof(*vps), 0, NULL, uninit_vps);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 }
468 468
469 vps = ff_refstruct_alloc_ext(sizeof(*vps), 0, NULL, hevc_vps_free);
469 if (!vps)470 if (!vps)
470 return AVERROR(ENOMEM);471 return AVERROR(ENOMEM);
471 472
472 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");473 av_log(avctx, AV_LOG_DEBUG, "Decoding VPS\n");
473 474
474 nal_size = gb->buffer_end - gb->buffer;475 vps->data_size = nal_size;
475 if (nal_size > sizeof(vps->data)) {476 vps->data = av_memdup(gb->buffer, nal_size);
476 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized VPS "477 if (!vps->data) {
477 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",478 ret = AVERROR(ENOMEM);
478 nal_size, sizeof(vps->data));479 goto err;
479 vps->data_size = sizeof(vps->data);
480 } else {
481 vps->data_size = nal_size;
482 }480 }
483 memcpy(vps->data, gb->buffer, vps->data_size);481 vps->vps_id = vps_id;
484
485 vps_id = vps->vps_id = get_bits(gb, 4);
486 482
487 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits483 if (get_bits(gb, 2) != 3) { // vps_reserved_three_2bits
488 av_log(avctx, AV_LOG_ERROR, "vps_reserved_three_2bits is not three\n");484 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
579 goto err;575 goto err;
580 }576 }
581 577
582 if (ps->vps_list[vps_id] &&578 remove_vps(ps, vps_id);
583 compare_vps(ps->vps_list[vps_id], vps)) {579 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 }
589 580
590 return 0;581 return 0;
591 582
592err:583err:
593 ff_refstruct_unref(&vps);584 ff_refstruct_unref(&vps);
594 return AVERROR_INVALIDDATA;585 return ret;
595}586}
596 587
597static void decode_vui(GetBitContext *gb, AVCodecContext *avctx,588static 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
1294 return 0;1285 return 0;
1295}1286}
1296 1287
1288static void hevc_sps_free(FFRefStructOpaque opaque, void *obj)
1289{
1290 HEVCSPS *sps = obj;
1291
1292 av_freep(&sps->data);
1293}
1294
1295static 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
1297int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,1301int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx,
1298 HEVCParamSets *ps, int apply_defdispwin)1302 HEVCParamSets *ps, int apply_defdispwin)
1299{1303{
1300 HEVCSPS *sps = ff_refstruct_allocz(sizeof(*sps));1304 HEVCSPS *sps = ff_refstruct_alloc_ext(sizeof(*sps), 0, NULL, hevc_sps_free);
1301 unsigned int sps_id;1305 unsigned int sps_id;
1302 int ret;1306 int ret;
1303 ptrdiff_t nal_size;
1304 1307
1305 if (!sps)1308 if (!sps)
1306 return AVERROR(ENOMEM);1309 return AVERROR(ENOMEM);
1307 1310
1308 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");1311 av_log(avctx, AV_LOG_DEBUG, "Decoding SPS\n");
1309 1312
1310 nal_size = gb->buffer_end - gb->buffer;1313 sps->data_size = gb->buffer_end - gb->buffer;
1311 if (nal_size > sizeof(sps->data)) {1314 sps->data = av_memdup(gb->buffer, sps->data_size);
1312 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized SPS "1315 if (!sps->data) {
1313 "(%"PTRDIFF_SPECIFIER" > %"SIZE_SPECIFIER")\n",1316 ret = AVERROR(ENOMEM);
1314 nal_size, sizeof(sps->data));1317 goto err;
1315 sps->data_size = sizeof(sps->data);
1316 } else {
1317 sps->data_size = nal_size;
1318 }1318 }
1319 memcpy(sps->data, gb->buffer, sps->data_size);
1320 1319
1321 ret = ff_hevc_parse_sps(sps, gb, &sps_id,1320 ret = ff_hevc_parse_sps(sps, gb, &sps_id,
1322 apply_defdispwin,1321 apply_defdispwin,
1323 ps->vps_list, avctx);1322 ps->vps_list, avctx);
1324 if (ret < 0) {1323 if (ret < 0)
1325 ff_refstruct_unref(&sps);1324 goto err;
1326 return ret;
1327 }
1328 1325
1329 if (avctx->debug & FF_DEBUG_BITSTREAM) {1326 if (avctx->debug & FF_DEBUG_BITSTREAM) {
1330 av_log(avctx, AV_LOG_DEBUG,1327 av_log(avctx, AV_LOG_DEBUG,
@@ -1340,7 +1337,7 @@ int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, · 9 unmodified lines
1340 * original one.1337 * original one.
1341 * otherwise drop all PPSes that depend on it */1338 * otherwise drop all PPSes that depend on it */
1342 if (ps->sps_list[sps_id] &&1339 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)) {
1344 ff_refstruct_unref(&sps);1341 ff_refstruct_unref(&sps);
1345 } else {1342 } else {
1346 remove_sps(ps, sps_id);1343 remove_sps(ps, sps_id);
@@ -1348,6 +1345,9 @@ int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, · 1 unmodified line
1348 }1345 }
1349 1346
1350 return 0;1347 return 0;
1348err:
1349 ff_refstruct_unref(&sps);
1350 return ret;
1351}1351}
1352 1352
1353static void hevc_pps_free(FFRefStructOpaque unused, void *obj)1353static void hevc_pps_free(FFRefStructOpaque unused, void *obj)
@@ -1364,6 +1364,7 @@ static void hevc_pps_free(FFRefStructOpaque unused, void *obj) · 10 unmodified lines
1364 av_freep(&pps->tile_pos_rs);1364 av_freep(&pps->tile_pos_rs);
1365 av_freep(&pps->tile_id);1365 av_freep(&pps->tile_id);
1366 av_freep(&pps->min_tb_addr_zs_tab);1366 av_freep(&pps->min_tb_addr_zs_tab);
1367 av_freep(&pps->data);
1367}1368}
1368 1369
1369static void colour_mapping_octants(GetBitContext *gb, HEVCPPS *pps, int inp_depth,1370static 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
1762 const HEVCSPS *sps = NULL;1763 const HEVCSPS *sps = NULL;
1763 const HEVCVPS *vps = NULL;1764 const HEVCVPS *vps = NULL;
1764 int i, ret = 0;1765 int i, ret = 0;
1765 unsigned int pps_id = 0;1766 ptrdiff_t nal_size = gb->buffer_end - gb->buffer;
1766 ptrdiff_t nal_size;1767 unsigned int pps_id = get_ue_golomb_long(gb);
1767 unsigned log2_parallel_merge_level_minus2;1768 unsigned log2_parallel_merge_level_minus2;
1769 HEVCPPS *pps;
1770
1771 av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");
1768 1772
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 }
1770 1777
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);
1771 if (!pps)1786 if (!pps)
1772 return AVERROR(ENOMEM);1787 return AVERROR(ENOMEM);
1773 1788
1774 av_log(avctx, AV_LOG_DEBUG, "Decoding PPS\n");1789 pps->data_size = nal_size;
1775 1790 pps->data = av_memdup(gb->buffer, nal_size);
1776 nal_size = gb->buffer_end - gb->buffer;1791 if (!pps->data) {
1777 if (nal_size > sizeof(pps->data)) {1792 ret = AVERROR_INVALIDDATA;
1778 av_log(avctx, AV_LOG_WARNING, "Truncating likely oversized PPS "1793 goto err;
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;
1784 }1794 }
1785 memcpy(pps->data, gb->buffer, pps->data_size);
1786 1795
1787 // Default values1796 // Default values
1788 pps->loop_filter_across_tiles_enabled_flag = 1;1797 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
1795 pps->log2_max_transform_skip_block_size = 2;1804 pps->log2_max_transform_skip_block_size = 2;
1796 1805
1797 // Coded parameters1806 // Coded parameters
1798 pps_id = pps->pps_id = get_ue_golomb_long(gb);1807 pps->pps_id = pps_id;
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 }
1804 pps->sps_id = get_ue_golomb_long(gb);1808 pps->sps_id = get_ue_golomb_long(gb);
1805 if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {1809 if (pps->sps_id >= HEVC_MAX_SPS_COUNT) {
1806 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", pps->sps_id);1810 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
172 int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1172 int vps_num_ticks_poc_diff_one; ///< vps_num_ticks_poc_diff_one_minus1 + 1
173 int vps_num_hrd_parameters;173 int vps_num_hrd_parameters;
174 174
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 */
179 HEVCHdrParams *hdr;175 HEVCHdrParams *hdr;
176
177 uint8_t *data;
178 int data_size;
180} HEVCVPS;179} HEVCVPS;
181 180
182typedef struct ScalingList {181typedef struct ScalingList {
@@ -299,7 +298,7 @@ typedef struct HEVCSPS { · 116 unmodified lines
299 298
300 int qp_bd_offset;299 int qp_bd_offset;
301 300
302 uint8_t data[4096];301 uint8_t *data;
303 int data_size;302 int data_size;
304} HEVCSPS;303} HEVCSPS;
305 304
@@ -434,7 +433,7 @@ typedef struct HEVCPPS { · 128 unmodified lines
434 int *min_tb_addr_zs; ///< MinTbAddrZS433 int *min_tb_addr_zs; ///< MinTbAddrZS
435 int *min_tb_addr_zs_tab;///< MinTbAddrZS434 int *min_tb_addr_zs_tab;///< MinTbAddrZS
436 435
437 uint8_t data[4096];436 uint8_t *data;
438 int data_size;437 int data_size;
439} HEVCPPS;438} HEVCPPS;
440 439

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