On 20/05/17 08:02, Anton Khirnov wrote:
> Quoting Mark Thompson (2017-05-14 23:24:15)
>> This is able to modify some header metadata found in the SPS/VUI,
>> and can also add/remove AUDs and insert user data in SEI NAL units.
>> ---
>>  doc/bitstream_filters.texi     |  47 +++++
>>  libavcodec/Makefile            |   2 +
>>  libavcodec/bitstream_filters.c |   1 +
>>  libavcodec/h264_metadata_bsf.c | 457 
>> +++++++++++++++++++++++++++++++++++++++++
>>  4 files changed, 507 insertions(+)
>>  create mode 100644 libavcodec/h264_metadata_bsf.c
>>
>> diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c
>> new file mode 100644
>> index 000000000..2e99c75d1
>> --- /dev/null
>> +++ b/libavcodec/h264_metadata_bsf.c
>> @@ -0,0 +1,457 @@
>> +/*
>> + * This file is part of Libav.
>> + *
>> + * Libav is free software; you can redistribute it and/or
>> + * modify it under the terms of the GNU Lesser General Public
>> + * License as published by the Free Software Foundation; either
>> + * version 2.1 of the License, or (at your option) any later version.
>> + *
>> + * Libav is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> + * Lesser General Public License for more details.
>> + *
>> + * You should have received a copy of the GNU Lesser General Public
>> + * License along with Libav; if not, write to the Free Software
>> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
>> USA
>> + */
>> +
>> +#include "libavutil/common.h"
>> +#include "libavutil/opt.h"
>> +
>> +#include "bsf.h"
>> +#include "cbs.h"
>> +#include "cbs_h264.h"
>> +#include "h264.h"
>> +#include "h264_sei.h"
>> +
>> +enum {
>> +    PASS,
>> +    INSERT,
>> +    REMOVE,
>> +};
>> +
>> +typedef struct H264MetadataContext {
> 
> Missing AVClass*, unless you did this intentionally (then you should
> feel ashamed).

Um, yeah, oops.  (The logging is totally broken in this version anyway because 
the log context isn't propagated correctly, I will fix all of that for the next 
iteration.)

>> +static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
>> +{
>> +    H264MetadataContext *ctx = bsf->priv_data;
>> +    AVPacket *in;
>> +    CodedBitstreamFragment *au = &ctx->access_unit;
>> +    int err, i, j, has_sps;
>> +
>> +    err = ff_bsf_get_packet(bsf, &in);
>> +    if (err < 0)
>> +        return err;
> 
> Seems to me in is leaked in pretty much every single error path. Or am I
> missing something?

I'll rewrite to do the error handling properly through this whole function.

>> +
>> +    err = ff_cbs_read_packet(&ctx->cbc, au, in);
>> +    if (err < 0) {
>> +        av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
>> +        return err;
>> +    }
>> +
>> +    if (au->nb_units == 0) {
>> +        av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
>> +        return AVERROR_INVALIDDATA;
>> +    }
>> +
>> +    // If an AUD is present, it must be the first NAL unit.
>> +    if (au->units[0].type == H264_NAL_AUD) {
>> +        if (ctx->aud == REMOVE)
>> +            ff_cbs_delete_unit(&ctx->cbc, au, 0);
>> +    } else {
>> +        if (ctx->aud == INSERT) {
>> +            int primary_pic_type_table[] = {
> 
> static const ?

Yes.

>> +                0x084, // 2, 7
>> +                0x0a5, // 0, 2, 5, 7
>> +                0x0e7, // 0, 1, 2, 5, 6, 7
>> +                0x210, // 4, 9
>> +                0x318, // 3, 4, 8, 9
>> +                0x294, // 2, 4, 7, 9
>> +                0x3bd, // 0, 2, 3, 4, 5, 7, 8, 9
>> +                0x3ff, // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
>> +            };
>> +            int primary_pic_type_mask = 0xff;
>> +            H264RawAUD *aud;
>> +
>> +            for (i = 0; i < au->nb_units; i++) {
>> +                if (au->units[i].type == H264_NAL_SLICE ||
>> +                    au->units[i].type == H264_NAL_IDR_SLICE) {
>> +                    H264RawSlice *slice = au->units[i].content;
>> +                    for (j = 0; j < 8; j++) {
> 
> FF_ARRAY_ELEMS(primary_pic_type_table)? Also below.

I guess?  There isn't really any opportunity for more values to be added given 
that it's a u(3) field, though.

>> +                         if (!(primary_pic_type_table[j] &
>> +                               (1 << slice->header.slice_type)))
>> +                             primary_pic_type_mask &= ~(1 << j);
>> +                    }
>> +                }
>> +            }
>> +            for (j = 0; j < 8; j++)
>> +                if (primary_pic_type_mask & (1 << j))
>> +                    break;
>> +            if (j >= 8) {
>> +                av_log(bsf, AV_LOG_ERROR, "No usable primary_pic_type: "
>> +                       "invalid slice types?\n");
>> +                err = AVERROR_INVALIDDATA;
>> +                goto fail;
>> +            }
>> +
>> +            aud = av_mallocz(sizeof(*aud));
>> +            if (!aud) {
>> +                err = AVERROR(ENOMEM);
>> +                goto fail;
>> +            }
>> +            aud->nal_ref_idc      = 0;
>> +            aud->nal_unit_type    = H264_NAL_AUD;
>> +            aud->primary_pic_type = j;
>> +
>> +            err = ff_cbs_insert_unit(&ctx->cbc, au,
>> +                                     0, H264_NAL_AUD, aud);
>> +            if (err) {
>> +                av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
>> +                goto fail;
> 
> Leaking aud.
> 
>> +            }
>> +        }
>> +    }
>> +
>> +    has_sps = 0;
>> +    for (i = 0; i < au->nb_units; i++) {
>> +        if (au->units[i].type == H264_NAL_SPS) {
>> +            h264_metadata_update_sps(bsf, au->units[i].content);
>> +            has_sps = 1;
>> +        }
>> +    }
>> +
>> +    // Only insert the SEI in access units containing SPSs.
>> +    if (has_sps && ctx->sei_user_data) {
>> +        H264RawSEI *sei;
>> +        H264RawSEIPayload *payload;
>> +        H264RawSEIUserDataUnregistered *udu;
>> +        int sei_pos;
>> +
>> +        for (i = 0; i < au->nb_units; i++) {
>> +            if (au->units[i].type == H264_NAL_SEI ||
>> +                au->units[i].type == H264_NAL_SLICE ||
>> +                au->units[i].type == H264_NAL_IDR_SLICE)
>> +                break;
>> +        }
>> +        sei_pos = i;
>> +
>> +        if (sei_pos < au->nb_units &&
>> +            au->units[sei_pos].type == H264_NAL_SEI) {
>> +            sei = au->units[sei_pos].content;
>> +        } else {
>> +            sei = av_mallocz(sizeof(*sei));
>> +            if (!sei) {
>> +                err = AVERROR(ENOMEM);
>> +                goto fail;
>> +            }
>> +            sei->nal_ref_idc   = 0;
>> +            sei->nal_unit_type = H264_NAL_SEI;
>> +
>> +            err = ff_cbs_insert_unit(&ctx->cbc, au,
>> +                                     sei_pos, H264_NAL_SEI, sei);
>> +            if (err < 0) {
>> +                av_log(bsf, AV_LOG_ERROR, "Failed to insert SEI.\n");
>> +                goto fail;
> 
> Leaking sei.
> 
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to