[FFmpeg-devel] avcodec/hapqa_extract_bsf : add bsf filter for haqqa (to hapq or hapalpha only) conversion

James Almer jamrial at gmail.com
Sun Mar 11 16:12:18 EET 2018


On 3/11/2018 10:47 AM, Martin Vignali wrote:
> Hello
> 
> New patchs in attach :
> - check texture type, for rgb or alpha keep (don't assume first texture is
> rgb and second texture is alpha)
> - change options, to use "color" or "alpha".
> - Translate french comments
> - add changelog entry
> 
> Not entirely sure, of the options part of the bsf filter.
> 
> Comments welcome
> 
> Martin

> +static int hapqa_extract(AVBSFContext *bsf, AVPacket *out)
> +{
> +    HapqaExtractContext *ctx = bsf->priv_data;
> +    GetByteContext gbc;
> +    int section_size;
> +    enum HapSectionType section_type;
> +    int start_section_size;
> +    int target_packet_size = 0;
> +    AVPacket *in;
> +    int ret = 0;
> +    uint8_t *out_buf;
> +
> +    av_log(bsf, AV_LOG_DEBUG, "Texture to keep : %d.\n", ctx->texture);

There's no point printing this on every packet. Either add an init()
callback function and put it there, or remove it altogether since it's
of dubious usefulness.

> +
> +    ret = ff_bsf_get_packet(bsf, &in);
> +    if (ret < 0)
> +        return ret;
> +
> +    bytestream2_init(&gbc, in->data, in->size);
> +    ret = ff_hap_parse_section_header(&gbc, &section_size, &section_type);
> +    if (ret != 0)
> +        goto fail;
> +
> +    if ((section_type & 0x0F) != 0x0D) {
> +        av_log(bsf, AV_LOG_ERROR, "Invalid section type for HAPQA %#04x.\n", section_type & 0x0F);
> +        goto fail;
> +    }
> +
> +    start_section_size = 4;
> +
> +    bytestream2_seek(&gbc, start_section_size, SEEK_SET);/* go to start of the first texture */
> +
> +    ret = ff_hap_parse_section_header(&gbc, &section_size, &section_type);
> +    if (ret != 0)
> +        goto fail;
> +
> +    target_packet_size = section_size + 4;
> +
> +    if (check_texture(ctx, section_type) == 0) { /* the texture is not the one to keep */
> +        start_section_size += 4 + section_size;
> +        bytestream2_seek(&gbc, start_section_size, SEEK_SET);/* go to start of the second texture */
> +        ret = ff_hap_parse_section_header(&gbc, &section_size, &section_type);
> +        if (ret != 0)
> +            goto fail;
> +
> +        target_packet_size = section_size + 4;
> +
> +        if (check_texture(ctx, section_type) == 0){ /* the second texture is not the one to keep */
> +            av_log(bsf, AV_LOG_ERROR, "No valid texture found.\n");
> +            goto fail;
> +        }
> +    }
> +


> +    ret = av_new_packet(out, target_packet_size);
> +    if (ret < 0)
> +        goto fail;
> +
> +    out_buf = out->data;
> +    bytestream_put_buffer(&out_buf, in->data + start_section_size, target_packet_size);
> +
> +    ret = av_packet_copy_props(out, in);
> +    if (ret < 0)
> +        goto fail;

Why not just do something like

av_packet_move_ref(out, in);

out->data += start_section_size;
out->size  = target_packet_size;

(Untested)

You're not assembling any complex data structure using separate parts of
the input packet, so no need to reallocate the entire buffer for this.
It will be faster this way.
See dca_core_bsf for another example of this.

> +
> +fail:
> +    if (ret < 0)
> +        av_packet_unref(out);
> +    av_packet_free(&in);
> +    return ret;
> +}
> +
> +static const enum AVCodecID codec_ids[] = {
> +    AV_CODEC_ID_HAP, AV_CODEC_ID_NONE,
> +};
> +
> +#define OFFSET(x) offsetof(HapqaExtractContext, x)
> +#define FLAGS     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
> +static const AVOption options[] = {
> +    { "texture", "texture to keep", OFFSET(texture), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS,  "texture" },
> +        { "color", "keep HapQ texture", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "texture" },
> +        { "alpha", "keep HapAlphaOnly texture", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "texture" },
> +    { NULL },
> +};
> +
> +static const AVClass hapqa_extract_class = {
> +    .class_name = "hapqa_extract_bsf",
> +    .item_name  = av_default_item_name,
> +    .option     = options,
> +    .version    = LIBAVUTIL_VERSION_MAJOR,
> +};
> +
> +const AVBitStreamFilter ff_hapqa_extract_bsf = {
> +    .name       = "hapqa_extract",
> +    .filter     = hapqa_extract,
> +    .priv_data_size = sizeof(HapqaExtractContext),
> +    .priv_class = &hapqa_extract_class,
> +    .codec_ids  = codec_ids,
> +};
> -- 
> 2.14.3 (Apple Git-98)
> 


More information about the ffmpeg-devel mailing list