From patchwork Fri Aug 17 14:12:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: joshdk@ob-encoder.com X-Patchwork-Id: 10022 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:104:0:0:0:0:0 with SMTP id c4-v6csp816502jad; Fri, 17 Aug 2018 07:19:23 -0700 (PDT) X-Google-Smtp-Source: AA+uWPyP+bqIBn3SMrnKhnsfhk2B8dLNK9MW8c35QuRughAl44k0Ri8B+Uduf05has1HDjYFxvBR X-Received: by 2002:a1c:1805:: with SMTP id 5-v6mr18068313wmy.25.1534515563281; Fri, 17 Aug 2018 07:19:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534515563; cv=none; d=google.com; s=arc-20160816; b=W2kgiMwzssqAsBQ/eyB1B9RanB/rzZ5lf4IO67ITMmS8Y4E+HYwJf25+AXYk4IOuMR tjggDRkZ9X70Fg9aybFei3zNMnk3fXABBDfOZRW7sY7Z5mzYdVmJ3vujPDNu2F+YxDMZ /88HHvqsj5sZscoHCWdOsy/g+XKbQnt6SPYPev+srIVy4DuTl4Ac5SF7OuYblQMJMRS4 s+25qw+DRe3phVjCVuPukdmKCGGWrTa5cZiRFtoFrb9ynB0jSbt+0ADLlmcrV5h0F4qi 4Nsv5Jf/5f3ofdl5WTmhM5W0dPWplhRl1ddsnLGdVNo6n3F4ySdUfsrM6HjPfprLXshj 9q9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=t0E64qWdkneDZvRRMDL8EBDD4p/dQIpy8riKbXwHE5w=; b=bbGF6eogazWgxXZG+W1dOlTO1BTUnLfLL4SGrjYLC3Fdt+WVRfGBr7N2IZ+RZJpcQR lRs00WWIOv8frt6oVDMS3zwg4N3rEF5reqs9HrdMaAZt6TM9+Q/fmFGegsZ2tEzacaXV pPdc0M1JYGVEqJHqZuZ13K4qg/g50KD3AN6qIUhSSCzilKimksYkYFNwDPXr5UPy6Bel kgkWyW/L0d8PNKLKC+VTbiH5/T0mM+MkTlrcFmVLa4wQdAl5EHE9q8X/uspB4jO93Lbs 9ve00VefvBPZdeKYoEZ1wNo4QvXAYr4/qZ7t2sA830K4yBakrobLoDewDu0uCmDUFXn7 Swmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@ob-encoder-com.20150623.gappssmtp.com header.s=20150623 header.b=B2LDTeFe; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 136-v6si3054185wmf.64.2018.08.17.07.19.22; Fri, 17 Aug 2018 07:19:23 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@ob-encoder-com.20150623.gappssmtp.com header.s=20150623 header.b=B2LDTeFe; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1720068A04C; Fri, 17 Aug 2018 17:18:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7D07E689F5F for ; Fri, 17 Aug 2018 17:18:48 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id b15-v6so7232423wrv.10 for ; Fri, 17 Aug 2018 07:19:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ob-encoder-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SbkqeSAeb/NJ+uuQEwVkB6EsWHOQ11HAi98+YP7gXkc=; b=B2LDTeFe7suVm139rToUeamfc7SJSs3BsNo5M7S3BfwVscvfCbsWqMf8Jcs+X9Zhu6 7n9oGK79CtZFtACdjiALWlIRfsTuLhbh8Td/GsGBPrA2GksT1PKvI+u9xxsrpzf7l/LQ g8h5K4GIHxVyKlVlD50d8pWme7KDRIrB6seoTZ4JfGkEfTkHnYa7zBiufGOzAQHH9Ss5 bkEm1nMnjbUuREGHoc9cN6p6Ql3rI7awZE++h+g+BjENnI676+Bht+A7UJKV9L35Vdn7 y9P6VIfcRRfXhY1EEAbzVIc0YgIAQKw89yskibZ5cU0cfeRQY7AUlGfF5KI0kdydrvi3 J4IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SbkqeSAeb/NJ+uuQEwVkB6EsWHOQ11HAi98+YP7gXkc=; b=Ge8DV0xY9NERm9zjNxRlWDXGRw/FD2EQkf1/DeWS1e83FMuo1q0OPfUZKQMCZQOE/S 52OiqWgTeMci++z4Nz7seL25qUq6/5V9Z+XtX2/9G9R90bsKyxN0CyBFOTjjF2ZLpOCH Y5o6icjKyCv7D9LXp+ioGqcAg2h0vNKLnHnhp6SDm7J9fI7pttEvZaBftVzcfkkuD2cD 0hm8Px8pr2LsDCRJevnEYmPiCMNirTAwWSXPDDaVxdHTSoSMeZIpepbz/DWhfOJuQj7y byQ7JKxyURdFuwTO4FTxB72fSwaALbn1aWUkhdbKc/g2zD+gnbPhqEkSMHtaRmOVxpPA u4Xw== X-Gm-Message-State: AOUpUlFyXsYAO8uzvP+WRFuSZ+hn6s2AIXAQ1ehvw9KaiQgKtb8bBBIj kaSIr7YQe9oDGxRNV+zIuIfhEKBsN0WLKQ== X-Received: by 2002:adf:9d46:: with SMTP id o6-v6mr22164017wre.51.1534515135962; Fri, 17 Aug 2018 07:12:15 -0700 (PDT) Received: from localhost.localdomain (cpc75394-sotn16-2-0-cust168.15-1.cable.virginm.net. [82.22.8.169]) by smtp.gmail.com with ESMTPSA id t6-v6sm5480663wmf.8.2018.08.17.07.12.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Aug 2018 07:12:15 -0700 (PDT) From: joshdk@ob-encoder.com X-Google-Original-From: joshdk@obe.tv To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 Aug 2018 15:12:14 +0100 Message-Id: <20180817141214.15789-1-joshdk@obe.tv> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: Subject: [FFmpeg-devel] [PATCH v3] h264: Support multi-field closed captions by using AVBufferRef and not resetting per field X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Josh de Kock , Kieran Kunhya MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Kieran Kunhya Signed-off-by: Josh de Kock --- libavcodec/h264_sei.c | 15 ++++++++------- libavcodec/h264_sei.h | 3 +-- libavcodec/h264_slice.c | 22 ++++++++++++++-------- libavcodec/h264dec.c | 5 +++-- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/libavcodec/h264_sei.c b/libavcodec/h264_sei.c index 6499086210..43593d34d2 100644 --- a/libavcodec/h264_sei.c +++ b/libavcodec/h264_sei.c @@ -51,8 +51,7 @@ void ff_h264_sei_uninit(H264SEIContext *h) h->display_orientation.present = 0; h->afd.present = 0; - h->a53_caption.a53_caption_size = 0; - av_freep(&h->a53_caption.a53_caption); + av_buffer_unref(&h->a53_caption.buf_ref); } static int decode_picture_timing(H264SEIPictureTiming *h, GetBitContext *gb, @@ -169,7 +168,8 @@ static int decode_registered_user_data_closed_caption(H264SEIA53Caption *h, size -= 2; if (cc_count && size >= cc_count * 3) { - const uint64_t new_size = (h->a53_caption_size + cc_count + int old_size = h->buf_ref ? h->buf_ref->size : 0; + const uint64_t new_size = (old_size + cc_count * UINT64_C(3)); int i, ret; @@ -177,14 +177,15 @@ static int decode_registered_user_data_closed_caption(H264SEIA53Caption *h, return AVERROR(EINVAL); /* Allow merging of the cc data from two fields. */ - ret = av_reallocp(&h->a53_caption, new_size); + ret = av_buffer_realloc(&h->buf_ref, new_size); if (ret < 0) return ret; + /* Use of av_buffer_realloc assumes buffer is writeable */ for (i = 0; i < cc_count; i++) { - h->a53_caption[h->a53_caption_size++] = get_bits(gb, 8); - h->a53_caption[h->a53_caption_size++] = get_bits(gb, 8); - h->a53_caption[h->a53_caption_size++] = get_bits(gb, 8); + h->buf_ref->data[old_size++] = get_bits(gb, 8); + h->buf_ref->data[old_size++] = get_bits(gb, 8); + h->buf_ref->data[old_size++] = get_bits(gb, 8); } skip_bits(gb, 8); // marker_bits diff --git a/libavcodec/h264_sei.h b/libavcodec/h264_sei.h index a169a10e49..5b7c8ef9d8 100644 --- a/libavcodec/h264_sei.h +++ b/libavcodec/h264_sei.h @@ -95,8 +95,7 @@ typedef struct H264SEIAFD { } H264SEIAFD; typedef struct H264SEIA53Caption { - int a53_caption_size; - uint8_t *a53_caption; + AVBufferRef *buf_ref; } H264SEIA53Caption; typedef struct H264SEIUnregistered { diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c index ede9a1a6ea..58e1aaf02f 100644 --- a/libavcodec/h264_slice.c +++ b/libavcodec/h264_slice.c @@ -430,6 +430,13 @@ int ff_h264_update_thread_context(AVCodecContext *dst, h->frame_recovered = h1->frame_recovered; + av_buffer_unref(&h->sei.a53_caption.buf_ref); + if (h1->sei.a53_caption.buf_ref) { + h->sei.a53_caption.buf_ref = av_buffer_ref(h1->sei.a53_caption.buf_ref); + if (!h->sei.a53_caption.buf_ref) + return AVERROR(ENOMEM); + } + if (!h->cur_pic_ptr) return 0; @@ -1269,15 +1276,14 @@ static int h264_export_frame_props(H264Context *h) } } - if (h->sei.a53_caption.a53_caption) { + if (h->sei.a53_caption.buf_ref) { H264SEIA53Caption *a53 = &h->sei.a53_caption; - AVFrameSideData *sd = av_frame_new_side_data(cur->f, - AV_FRAME_DATA_A53_CC, - a53->a53_caption_size); - if (sd) - memcpy(sd->data, a53->a53_caption, a53->a53_caption_size); - av_freep(&a53->a53_caption); - a53->a53_caption_size = 0; + + AVFrameSideData *sd = av_frame_new_side_data_from_buf(cur->f, AV_FRAME_DATA_A53_CC, a53->buf_ref); + if (!sd) + av_buffer_unref(&a53->buf_ref); + a53->buf_ref = NULL; + h->avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS; } diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c index 7494c7a8f2..8d115fa040 100644 --- a/libavcodec/h264dec.c +++ b/libavcodec/h264dec.c @@ -609,9 +609,10 @@ static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size) if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) { h->current_slice = 0; - if (!h->first_field) + if (!h->first_field) { h->cur_pic_ptr = NULL; - ff_h264_sei_uninit(&h->sei); + ff_h264_sei_uninit(&h->sei); + } } if (h->nal_length_size == 4) {