[libav-devel] [PATCH 1/2 V6] libavfilter/overlay_qsv: Add QSV overlay vpp filter

2017-08-10 Thread Huang, Zhengxu

---
Changes from v5:
* Initialize the AVExpr variables to NULL as Anton's review.
  ---
---
Changes from v4:
* Modify the configure file/coding style/system memory input surface 
align issue/resource release issue when exception happens/memory mode 
logic and


  some potential problem in the code as Anton's review.
  ---
---
Changes from v3:
* Modify the configure file as Maxym's review.
  ---
---
Changes from v2:
* Add "SKIPHEADERS" and modify some comments/typo/coding style as Luca's 
review .

  ---
---
Changes from v1:
* Replace "enum QSVMemMode" with MFX_MEMTYPE_* type which already have 
been defined in MSDK header file as per Maxym's review .

* Add some comments about the overlay usage.
  ---

From c4e8d8c22f2100bd9bffaccca628c5bd3bfd7281 Mon Sep 17 00:00:00 2001
From: "Huang, Zhengxu" 
Date: Tue, 25 Jul 2017 21:55:50 +0800
Subject: [PATCH] libavfilter/overlay_qsv: Add QSV overlay vpp filter

Add intel QSV overlay filter. Now it supports two input. And it also supports
the second input scale(implicit) during composition compared to the sw overlay.

Code has been separated into common interface part and qsv overlay implement 
part.
The common part mainly creates the qsv session and manages the surface which are
nearly the same for all qsv filters. So the qsvvpp.c/qsvvpp.h API can be used by
other QSV vpp filters to reduce code redundancy.

Usage:
 -hwaccel qsv -c:v mpeg2_qsv -r 25 -i in.m2v -hwaccel qsv -c:v h264_qsv -i 
in.h264 -filter_complex
"overlay_qsv=eof_action=repeat:x=(W-w)/2:y=(H-h)/2"  -b 2M -maxrate 3M  -c:v 
h264_qsv -y out.h264

two input should have different size otherwise one will be completely covered 
or you need scale the
second input as follow:
 -hwaccel qsv -c:v mpeg2_qsv -r 25 -i in.m2v -hwaccel qsv -c:v h264_qsv -i 
in.h264 -filter_complex
"overlay_qsv=w=720:h=576:x=(W-w)/2:y=(H-h)/2" -b 2M -maxrate 3M -c:v h264_qsv 
-y out.h264

Signed-off-by: ChaoX A Liu 
Signed-off-by: Zhengxu Huang 
Signed-off-by: Andrew Zhang 
Change-Id: I5c381febb0af6e2f9622c54ba00490ab99d48297
---
 Changelog|   1 +
 configure|   4 +
 libavfilter/Makefile |   6 +
 libavfilter/allfilters.c |   1 +
 libavfilter/qsvvpp.c | 737 +++
 libavfilter/qsvvpp.h |  64 
 libavfilter/vf_overlay_qsv.c | 493 +
 7 files changed, 1306 insertions(+)
 create mode 100644 libavfilter/qsvvpp.c
 create mode 100644 libavfilter/qsvvpp.h
 create mode 100644 libavfilter/vf_overlay_qsv.c

diff --git a/Changelog b/Changelog
index 92a7249..51c3f85 100644
--- a/Changelog
+++ b/Changelog
@@ -19,6 +19,7 @@ version :
 - Cinepak encoder
 - Intel QSV-accelerated MJPEG encoding
 - NVIDIA CUVID-accelerated H.264 and HEVC decoding
+- Intel QSV-accelerated overlay filter
 
 
 version 12:
diff --git a/configure b/configure
index 4510100..e550833 100755
--- a/configure
+++ b/configure
@@ -1786,6 +1786,7 @@ CONFIG_EXTRA="
 qsv
 qsvdec
 qsvenc
+qsvvpp
 rangecoder
 riffdec
 riffenc
@@ -2269,6 +2270,7 @@ omx_rpi_select="omx"
 qsv_deps="libmfx"
 qsvdec_select="qsv"
 qsvenc_select="qsv"
+qsvvpp_select="qsv"
 vaapi_encode_deps="vaapi"
 
 hwupload_cuda_filter_deps="cuda"
@@ -2528,6 +2530,8 @@ hqdn3d_filter_deps="gpl"
 interlace_filter_deps="gpl"
 movie_filter_deps="avcodec avformat"
 ocv_filter_deps="libopencv"
+overlay_qsv_filter_deps="libmfx"
+overlay_qsv_filter_select="qsvvpp"
 resample_filter_deps="avresample"
 scale_filter_deps="swscale"
 scale_qsv_filter_deps="libmfx"
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 348ad92..1f25180 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -20,6 +20,9 @@ OBJS = allfilters.o   
  \
 
 OBJS-$(HAVE_THREADS) += pthread.o
 
+# subsystems
+OBJS-$(CONFIG_QSVVPP)+= qsvvpp.o
+
 # audio filters
 OBJS-$(CONFIG_AFORMAT_FILTER)+= af_aformat.o
 OBJS-$(CONFIG_AMIX_FILTER)   += af_amix.o
@@ -75,6 +78,7 @@ OBJS-$(CONFIG_NOFORMAT_FILTER)   += vf_format.o
 OBJS-$(CONFIG_NULL_FILTER)   += vf_null.o
 OBJS-$(CONFIG_OCV_FILTER)+= vf_libopencv.o
 OBJS-$(CONFIG_OVERLAY_FILTER)+= vf_overlay.o
+OBJS-$(CONFIG_OVERLAY_QSV_FILTER)+= vf_overlay_qsv.o
 OBJS-$(CONFIG_PAD_FILTER)+= vf_pad.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)+= vf_pixdesctest.o
 OBJS-$(CONFIG_SCALE_FILTER)  += vf_scale.o
@@ -104,5 +108,7 @@ OBJS-$(CONFIG_NULLSRC_FILTER)+= 
vsrc_nullsrc.o
 OBJS-$(CONFIG_RGBTESTSRC_FILTER) += vsrc_testsrc.o
 OBJS-$(CONFIG_TESTSRC_FILTER)+= vsrc_testsrc.o
 
+SKIPHEADERS-$(CONFIG_QSVVPP) += qsvvpp.h
+
 TOOLS = graph2dot
 TESTPROGS = filtfmts

[libav-devel] [PATCH 08/14] vaapi_h264: Convert to use coded bitstream infrastructure

2017-08-10 Thread Mark Thompson
---
Just updated build system.


 configure  |2 +-
 libavcodec/Makefile|2 +-
 libavcodec/vaapi_encode_h264.c | 1421 
 3 files changed, 549 insertions(+), 876 deletions(-)

diff --git a/configure b/configure
index dfa3867ac..215e0eaa9 100755
--- a/configure
+++ b/configure
@@ -2285,7 +2285,7 @@ h264_omx_encoder_deps="omx"
 h264_qsv_decoder_select="h264_mp4toannexb_bsf h264_parser qsvdec 
h264_qsv_hwaccel"
 h264_qsv_encoder_select="qsvenc"
 h264_vaapi_encoder_deps="VAEncPictureParameterBufferH264"
-h264_vaapi_encoder_select="vaapi_encode golomb"
+h264_vaapi_encoder_select="vaapi_encode cbs_h264"
 hevc_nvenc_encoder_deps="nvenc"
 hevc_qsv_decoder_select="hevc_mp4toannexb_bsf hevc_parser hevc_qsv_hwaccel 
qsvdec"
 hevc_qsv_encoder_select="hevc_ps qsvenc"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index e1ae1e451..456ee9bb0 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -275,7 +275,7 @@ OBJS-$(CONFIG_H264_NVENC_ENCODER)  += nvenc_h264.o
 OBJS-$(CONFIG_H264_OMX_ENCODER)+= omx.o
 OBJS-$(CONFIG_H264_QSV_DECODER)+= qsvdec_h2645.o
 OBJS-$(CONFIG_H264_QSV_ENCODER)+= qsvenc_h264.o
-OBJS-$(CONFIG_H264_VAAPI_ENCODER)  += vaapi_encode_h264.o 
vaapi_encode_h26x.o
+OBJS-$(CONFIG_H264_VAAPI_ENCODER)  += vaapi_encode_h264.o
 OBJS-$(CONFIG_HAP_DECODER) += hapdec.o hap.o
 OBJS-$(CONFIG_HAP_ENCODER) += hapenc.o hap.o
 OBJS-$(CONFIG_HEVC_DECODER)+= hevcdec.o hevc_mvs.o hevc_sei.o \
diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c
index e08cf6116..b48070c95 100644
--- a/libavcodec/vaapi_encode_h264.c
+++ b/libavcodec/vaapi_encode_h264.c
@@ -16,128 +16,36 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include 
+
 #include 
 #include 
 
 #include "libavutil/avassert.h"
+#include "libavutil/common.h"
 #include "libavutil/internal.h"
 #include "libavutil/opt.h"
-#include "libavutil/pixfmt.h"
 
 #include "avcodec.h"
+#include "cbs.h"
+#include "cbs_h264.h"
 #include "h264.h"
 #include "h264_sei.h"
 #include "internal.h"
 #include "vaapi_encode.h"
-#include "vaapi_encode_h26x.h"
 
 enum {
-SLICE_TYPE_P  = 0,
-SLICE_TYPE_B  = 1,
-SLICE_TYPE_I  = 2,
-SLICE_TYPE_SP = 3,
-SLICE_TYPE_SI = 4,
+SEI_TIMING = 0x01,
+SEI_IDENTIFIER = 0x02,
 };
 
-// This structure contains all possibly-useful per-sequence syntax elements
-// which are not already contained in the various VAAPI structures.
-typedef struct VAAPIEncodeH264MiscSequenceParams {
-unsigned int profile_idc;
-char constraint_set0_flag;
-char constraint_set1_flag;
-char constraint_set2_flag;
-char constraint_set3_flag;
-char constraint_set4_flag;
-char constraint_set5_flag;
-
-char separate_colour_plane_flag;
-char qpprime_y_zero_transform_bypass_flag;
-
-char gaps_in_frame_num_allowed_flag;
-char delta_pic_order_always_zero_flag;
-char bottom_field_pic_order_in_frame_present_flag;
-
-unsigned int num_slice_groups_minus1;
-unsigned int slice_group_map_type;
-
-int pic_init_qs_minus26;
-
-char overscan_info_present_flag;
-char overscan_appropriate_flag;
-
-char video_signal_type_present_flag;
-unsigned int video_format;
-char video_full_range_flag;
-char colour_description_present_flag;
-unsigned int colour_primaries;
-unsigned int transfer_characteristics;
-unsigned int matrix_coefficients;
-
-char chroma_loc_info_present_flag;
-unsigned int chroma_sample_loc_type_top_field;
-unsigned int chroma_sample_loc_type_bottom_field;
-
-// Some timing elements are in VAEncSequenceParameterBufferH264.
-char fixed_frame_rate_flag;
-
-char nal_hrd_parameters_present_flag;
-char vcl_hrd_parameters_present_flag;
-char low_delay_hrd_flag;
-char pic_struct_present_flag;
-
-char motion_vectors_over_pic_boundaries_flag;
-unsigned int max_bytes_per_pic_denom;
-unsigned int max_bits_per_mb_denom;
-unsigned int max_num_reorder_frames;
-unsigned int max_dec_pic_buffering;
-
-unsigned int cpb_cnt_minus1;
-unsigned int bit_rate_scale;
-unsigned int cpb_size_scale;
-unsigned int bit_rate_value_minus1[32];
-unsigned int cpb_size_value_minus1[32];
-char cbr_flag[32];
-unsigned int initial_cpb_removal_delay_length_minus1;
-unsigned int cpb_removal_delay_length_minus1;
-unsigned int dpb_output_delay_length_minus1;
-unsigned int time_offset_length;
-
-unsigned int initial_cpb_removal_delay;
-unsigned int initial_cpb_removal_delay_offset;
-
-unsigned int pic_struct;
-} VAAPIEncodeH264MiscSequenceParams;
-
-// This structure contains all possibly-useful per-slice syntax elements
-// which are not already contained in the various VAAPI structures.
-typedef struct VAAPIEncodeH264MiscSliceParams {
-unsigned int nal_unit_type;
-

[libav-devel] [PATCH 14/14] hevc: Remove unused hevc_ps_enc.c

2017-08-10 Thread Mark Thompson
Replaced with more complete implementation via coded bitstream infrastructure.
---
Unchanged.


 libavcodec/hevc_ps.h |   3 --
 libavcodec/hevc_ps_enc.c | 118 ---
 2 files changed, 121 deletions(-)
 delete mode 100644 libavcodec/hevc_ps_enc.c

diff --git a/libavcodec/hevc_ps.h b/libavcodec/hevc_ps.h
index 6e2b52777..7f88b42e1 100644
--- a/libavcodec/hevc_ps.h
+++ b/libavcodec/hevc_ps.h
@@ -312,7 +312,4 @@ int ff_hevc_decode_nal_pps(GetBitContext *gb, 
AVCodecContext *avctx,
 int ff_hevc_decode_short_term_rps(GetBitContext *gb, AVCodecContext *avctx,
   ShortTermRPS *rps, const HEVCSPS *sps, int 
is_slice_header);
 
-int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
-   uint8_t *buf, int buf_size);
-
 #endif /* AVCODEC_HEVC_PS_H */
diff --git a/libavcodec/hevc_ps_enc.c b/libavcodec/hevc_ps_enc.c
deleted file mode 100644
index 1fb93b302..0
--- a/libavcodec/hevc_ps_enc.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * HEVC Parameter Set encoding
- *
- * 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 "golomb_legacy.h"
-#include "hevc_ps.h"
-#include "put_bits.h"
-
-static void write_ptl_layer(PutBitContext *pb, PTLCommon *ptl)
-{
-int i;
-
-put_bits(pb, 2, ptl->profile_space);
-put_bits(pb, 1, ptl->tier_flag);
-put_bits(pb, 5, ptl->profile_idc);
-for (i = 0; i < 32; i++)
-put_bits(pb, 1, ptl->profile_compatibility_flag[i]);
-put_bits(pb, 1, ptl->progressive_source_flag);
-put_bits(pb, 1, ptl->interlaced_source_flag);
-put_bits(pb, 1, ptl->non_packed_constraint_flag);
-put_bits(pb, 1, ptl->frame_only_constraint_flag);
-put_bits32(pb, 0);   // reserved
-put_bits(pb, 12, 0); // reserved
-}
-
-static void write_ptl(PutBitContext *pb, PTL *ptl, int max_num_sub_layers)
-{
-int i;
-
-write_ptl_layer(pb, >general_ptl);
-put_bits(pb, 8, ptl->general_ptl.level_idc);
-
-for (i = 0; i < max_num_sub_layers - 1; i++) {
-put_bits(pb, 1, ptl->sub_layer_profile_present_flag[i]);
-put_bits(pb, 1, ptl->sub_layer_level_present_flag[i]);
-}
-
-if (max_num_sub_layers > 1)
-for (i = max_num_sub_layers - 1; i < 8; i++)
-put_bits(pb, 2, 0); // reserved
-
-for (i = 0; i < max_num_sub_layers - 1; i++) {
-if (ptl->sub_layer_profile_present_flag[i])
-write_ptl_layer(pb, >sub_layer_ptl[i]);
-if (ptl->sub_layer_level_present_flag[i])
-put_bits(pb, 8, ptl->sub_layer_ptl[i].level_idc);
-}
-}
-
-int ff_hevc_encode_nal_vps(HEVCVPS *vps, unsigned int id,
-   uint8_t *buf, int buf_size)
-{
-PutBitContext pb;
-int i;
-
-init_put_bits(, buf, buf_size);
-put_bits(,  4, id);
-put_bits(,  2, 3);   // reserved
-put_bits(,  6, vps->vps_max_layers - 1);
-put_bits(,  3, vps->vps_max_sub_layers - 1);
-put_bits(,  1, vps->vps_temporal_id_nesting_flag);
-put_bits(, 16, 0x);  // reserved
-
-write_ptl(, >ptl, vps->vps_max_sub_layers);
-
-put_bits(, 1, vps->vps_sub_layer_ordering_info_present_flag);
-for (i = vps->vps_sub_layer_ordering_info_present_flag ? 0 : 
vps->vps_max_layers - 1;
- i < vps->vps_max_sub_layers; i++) {
-set_ue_golomb(, vps->vps_max_dec_pic_buffering[i] - 1);
-set_ue_golomb(, vps->vps_num_reorder_pics[i]);
-set_ue_golomb(, vps->vps_max_latency_increase[i] + 1);
-}
-
-put_bits(, 6, vps->vps_max_layer_id);
-set_ue_golomb(, vps->vps_num_layer_sets - 1);
-
-if (vps->vps_num_layer_sets > 1) {
-avpriv_report_missing_feature(NULL, "Writing layer_id_included_flag");
-return AVERROR_PATCHWELCOME;
-}
-
-put_bits(, 1, vps->vps_timing_info_present_flag);
-if (vps->vps_timing_info_present_flag) {
-put_bits32(, vps->vps_num_units_in_tick);
-put_bits32(, vps->vps_time_scale);
-put_bits(, 1, vps->vps_poc_proportional_to_timing_flag);
-if (vps->vps_poc_proportional_to_timing_flag)
-set_ue_golomb(, vps->vps_num_ticks_poc_diff_one - 1);
-
-if (vps->vps_num_hrd_parameters) {
-avpriv_report_missing_feature(NULL, 

[libav-devel] [PATCH 10/14] vaapi_h264: Add support for SEI recovery points

2017-08-10 Thread Mark Thompson
Included by default with non-IDR intra frames.
---
Unchanged.


 libavcodec/vaapi_encode_h264.c | 20 +++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c
index 4f0e879e9..271644ebb 100644
--- a/libavcodec/vaapi_encode_h264.c
+++ b/libavcodec/vaapi_encode_h264.c
@@ -37,6 +37,7 @@
 enum {
 SEI_TIMING = 0x01,
 SEI_IDENTIFIER = 0x02,
+SEI_RECOVERY_POINT = 0x04,
 };
 
 // Random (version 4) ISO 11578 UUID.
@@ -61,6 +62,7 @@ typedef struct VAAPIEncodeH264Context {
 
 H264RawSEIBufferingPeriod buffering_period;
 H264RawSEIPicTiming pic_timing;
+H264RawSEIRecoveryPoint recovery_point;
 H264RawSEIUserDataUnregistered identifier;
 char *identifier_string;
 
@@ -230,6 +232,11 @@ static int 
vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
 priv->sei.payload[i].payload.pic_timing = priv->pic_timing;
 ++i;
 }
+if (opt->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
+priv->sei.payload[i].payload_type = H264_SEI_TYPE_RECOVERY_POINT;
+priv->sei.payload[i].payload.recovery_point = priv->recovery_point;
+++i;
+}
 
 priv->sei.payload_count = i;
 av_assert0(priv->sei.payload_count > 0);
@@ -613,6 +620,14 @@ static int 
vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
 priv->sei_needed = 1;
 }
 
+if (opt->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
+priv->recovery_point.recovery_frame_cnt = 0;
+priv->recovery_point.exact_match_flag   = 1;
+priv->recovery_point.broken_link_flag   = ctx->b_per_p > 0;
+
+priv->sei_needed = 1;
+}
+
 vpic->CurrPic = (VAPictureH264) {
 .picture_id  = pic->recon_surface,
 .frame_idx   = priv->frame_num,
@@ -950,7 +965,7 @@ static const AVOption vaapi_encode_h264_options[] = {
 
 { "sei", "Set SEI to include",
   OFFSET(sei), AV_OPT_TYPE_FLAGS,
-  { .i64 = SEI_IDENTIFIER | SEI_TIMING },
+  { .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT },
   0, INT_MAX, FLAGS, "sei" },
 { "identifier", "Include encoder version identifier",
   0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
@@ -958,6 +973,9 @@ static const AVOption vaapi_encode_h264_options[] = {
 { "timing", "Include timing parameters (buffering_period and pic_timing)",
   0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
   INT_MIN, INT_MAX, FLAGS, "sei" },
+{ "recovery_point", "Include recovery points where appropriate",
+  0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
+  INT_MIN, INT_MAX, FLAGS, "sei" },
 { NULL },
 };
 
-- 
2.11.0

___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

[libav-devel] [PATCH 11/14] vaapi_h265: Convert to use coded bitstream infrastructure

2017-08-10 Thread Mark Thompson
Also improves the metadata and generally makes the configuration
a bit cleaner.
---
Build system; also fix a silly bug with num_ticks_poc_diff noticed by 
hevc_metadata testing.


 configure  |2 +-
 libavcodec/Makefile|2 +-
 libavcodec/vaapi_encode_h265.c | 1544 +++-
 libavcodec/vaapi_encode_h26x.c |   68 --
 libavcodec/vaapi_encode_h26x.h |   45 --
 5 files changed, 590 insertions(+), 1071 deletions(-)
 delete mode 100644 libavcodec/vaapi_encode_h26x.c
 delete mode 100644 libavcodec/vaapi_encode_h26x.h

diff --git a/configure b/configure
index 215e0eaa9..c62165887 100755
--- a/configure
+++ b/configure
@@ -2290,7 +2290,7 @@ hevc_nvenc_encoder_deps="nvenc"
 hevc_qsv_decoder_select="hevc_mp4toannexb_bsf hevc_parser hevc_qsv_hwaccel 
qsvdec"
 hevc_qsv_encoder_select="hevc_ps qsvenc"
 hevc_vaapi_encoder_deps="VAEncPictureParameterBufferHEVC"
-hevc_vaapi_encoder_select="vaapi_encode golomb"
+hevc_vaapi_encoder_select="vaapi_encode cbs_h265"
 mjpeg_qsv_encoder_deps="libmfx"
 mjpeg_qsv_encoder_select="qsvenc"
 mjpeg_vaapi_encoder_deps="VAEncPictureParameterBufferJPEG"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 456ee9bb0..ffd17c9cf 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -285,7 +285,7 @@ OBJS-$(CONFIG_HEVC_NVENC_ENCODER)  += nvenc_hevc.o
 OBJS-$(CONFIG_HEVC_QSV_DECODER)+= qsvdec_h2645.o
 OBJS-$(CONFIG_HEVC_QSV_ENCODER)+= qsvenc_hevc.o hevc_ps_enc.o   \
   h2645_parse.o hevc_data.o
-OBJS-$(CONFIG_HEVC_VAAPI_ENCODER)  += vaapi_encode_h265.o 
vaapi_encode_h26x.o
+OBJS-$(CONFIG_HEVC_VAAPI_ENCODER)  += vaapi_encode_h265.o
 OBJS-$(CONFIG_HNM4_VIDEO_DECODER)  += hnm4video.o
 OBJS-$(CONFIG_HQ_HQA_DECODER)  += hq_hqa.o hq_hqadata.o hq_hqadsp.o \
   canopus.o
diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c
index 9b029e2e2..f441e0370 100644
--- a/libavcodec/vaapi_encode_h265.c
+++ b/libavcodec/vaapi_encode_h265.c
@@ -16,164 +16,25 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include 
+
 #include 
 #include 
 
 #include "libavutil/avassert.h"
-#include "libavutil/internal.h"
+#include "libavutil/common.h"
 #include "libavutil/opt.h"
-#include "libavutil/pixfmt.h"
 
 #include "avcodec.h"
+#include "cbs.h"
+#include "cbs_h265.h"
 #include "hevc.h"
 #include "internal.h"
 #include "put_bits.h"
 #include "vaapi_encode.h"
-#include "vaapi_encode_h26x.h"
-
-
-#define MAX_ST_REF_PIC_SETS  32
-#define MAX_DPB_PICS 16
-#define MAX_LAYERS1
-
-
-typedef struct VAAPIEncodeH265STRPS {
-char inter_ref_pic_set_prediction_flag;
-
-unsigned int num_negative_pics;
-unsigned int num_positive_pics;
-
-unsigned int delta_poc_s0_minus1[MAX_DPB_PICS];
-char used_by_curr_pic_s0_flag[MAX_DPB_PICS];
-
-unsigned int delta_poc_s1_minus1[MAX_DPB_PICS];
-char used_by_curr_pic_s1_flag[MAX_DPB_PICS];
-} VAAPIEncodeH265STRPS;
-
-// This structure contains all possibly-useful per-sequence syntax elements
-// which are not already contained in the various VAAPI structures.
-typedef struct VAAPIEncodeH265MiscSequenceParams {
-
-// Parameter set IDs.
-unsigned int video_parameter_set_id;
-unsigned int seq_parameter_set_id;
-
-// Layering.
-unsigned int vps_max_layers_minus1;
-unsigned int vps_max_sub_layers_minus1;
-char vps_temporal_id_nesting_flag;
-unsigned int vps_max_layer_id;
-unsigned int vps_num_layer_sets_minus1;
-unsigned int sps_max_sub_layers_minus1;
-char sps_temporal_id_nesting_flag;
-char layer_id_included_flag[MAX_LAYERS][64];
-
-// Profile/tier/level parameters.
-char general_profile_compatibility_flag[32];
-char general_progressive_source_flag;
-char general_interlaced_source_flag;
-char general_non_packed_constraint_flag;
-char general_frame_only_constraint_flag;
-char general_inbld_flag;
-
-// Decode/display ordering parameters.
-unsigned int log2_max_pic_order_cnt_lsb_minus4;
-char vps_sub_layer_ordering_info_present_flag;
-unsigned int vps_max_dec_pic_buffering_minus1[MAX_LAYERS];
-unsigned int vps_max_num_reorder_pics[MAX_LAYERS];
-unsigned int vps_max_latency_increase_plus1[MAX_LAYERS];
-char sps_sub_layer_ordering_info_present_flag;
-unsigned int sps_max_dec_pic_buffering_minus1[MAX_LAYERS];
-unsigned int sps_max_num_reorder_pics[MAX_LAYERS];
-unsigned int sps_max_latency_increase_plus1[MAX_LAYERS];
-
-// Timing information.
-char vps_timing_info_present_flag;
-unsigned int vps_num_units_in_tick;
-unsigned int vps_time_scale;
-char vps_poc_proportional_to_timing_flag;
-unsigned int vps_num_ticks_poc_diff_minus1;
-
-// Cropping information.
-char conformance_window_flag;
-unsigned int conf_win_left_offset;
-unsigned int 

[libav-devel] [PATCH 12/14] vaapi_h265: Add support for AUD NAL units

2017-08-10 Thread Mark Thompson
Matching the H.264 encoder.
---
Unchanged.


 libavcodec/vaapi_encode_h265.c | 39 +++
 1 file changed, 39 insertions(+)

diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c
index f441e0370..0ebeeb693 100644
--- a/libavcodec/vaapi_encode_h265.c
+++ b/libavcodec/vaapi_encode_h265.c
@@ -42,6 +42,7 @@ typedef struct VAAPIEncodeH265Context {
 int fixed_qp_p;
 int fixed_qp_b;
 
+H265RawAUD aud;
 H265RawVPS vps;
 H265RawSPS sps;
 H265RawPPS pps;
@@ -52,13 +53,16 @@ typedef struct VAAPIEncodeH265Context {
 
 int slice_nal_unit;
 int slice_type;
+int pic_type;
 
 CodedBitstreamContext cbc;
 CodedBitstreamFragment current_access_unit;
+int aud_needed;
 } VAAPIEncodeH265Context;
 
 typedef struct VAAPIEncodeH265Options {
 int qp;
+int aud;
 } VAAPIEncodeH265Options;
 
 
@@ -117,6 +121,13 @@ static int 
vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
 CodedBitstreamFragment   *au = >current_access_unit;
 int err;
 
+if (priv->aud_needed) {
+err = vaapi_encode_h265_add_nal(avctx, au, >aud);
+if (err < 0)
+goto fail;
+priv->aud_needed = 0;
+}
+
 err = vaapi_encode_h265_add_nal(avctx, au, >vps);
 if (err < 0)
 goto fail;
@@ -145,6 +156,13 @@ static int 
vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
 CodedBitstreamFragment   *au = >current_access_unit;
 int err;
 
+if (priv->aud_needed) {
+err = vaapi_encode_h265_add_nal(avctx, au, >aud);
+if (err < 0)
+goto fail;
+priv->aud_needed = 0;
+}
+
 err = vaapi_encode_h265_add_nal(avctx, au, >slice);
 if (err < 0)
 goto fail;
@@ -519,6 +537,7 @@ static int 
vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
 {
 VAAPIEncodeContext   *ctx = avctx->priv_data;
 VAAPIEncodeH265Context  *priv = ctx->priv_data;
+VAAPIEncodeH265Options   *opt = ctx->codec_options;
 VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
 int i;
 
@@ -529,16 +548,19 @@ static int 
vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
 
 priv->slice_nal_unit = HEVC_NAL_IDR_W_RADL;
 priv->slice_type = HEVC_SLICE_I;
+priv->pic_type   = 0;
 } else {
 av_assert0(pic->encode_order > priv->last_idr_frame);
 
 if (pic->type == PICTURE_TYPE_I) {
 priv->slice_nal_unit = HEVC_NAL_CRA_NUT;
 priv->slice_type = HEVC_SLICE_I;
+priv->pic_type   = 0;
 } else if (pic->type == PICTURE_TYPE_P) {
 av_assert0(pic->refs[0]);
 priv->slice_nal_unit = HEVC_NAL_TRAIL_R;
 priv->slice_type = HEVC_SLICE_P;
+priv->pic_type   = 1;
 } else {
 av_assert0(pic->refs[0] && pic->refs[1]);
 if (pic->refs[1]->type == PICTURE_TYPE_I)
@@ -546,10 +568,23 @@ static int 
vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
 else
 priv->slice_nal_unit = HEVC_NAL_TRAIL_N;
 priv->slice_type = HEVC_SLICE_B;
+priv->pic_type   = 2;
 }
 }
 priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
 
+if (opt->aud) {
+priv->aud_needed = 1;
+priv->aud.nal_unit_header = (H265RawNALUnitHeader) {
+.nal_unit_type = HEVC_NAL_AUD,
+.nuh_layer_id  = 0,
+.nuh_temporal_id_plus1 = 1,
+};
+priv->aud.pic_type = priv->pic_type;
+} else {
+priv->aud_needed = 0;
+}
+
 vpic->decoded_curr_pic = (VAPictureHEVC) {
 .picture_id= pic->recon_surface,
 .pic_order_cnt = priv->pic_order_cnt,
@@ -905,6 +940,10 @@ static av_cold int vaapi_encode_h265_close(AVCodecContext 
*avctx)
 static const AVOption vaapi_encode_h265_options[] = {
 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
   OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
+
+{ "aud", "Include AUD",
+  OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
+
 { NULL },
 };
 
-- 
2.11.0

___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

[libav-devel] [PATCH 13/14] qsvenc_hevc: Replace ad-hoc VPS writing with CBS implementation

2017-08-10 Thread Mark Thompson
This copies more information which should be present from the SPS.
It also fixes the value of vps_temporal_id_nesting_flag, which was
previously incorrect for a single-layer stream (the standard states
that it must be 1, and the reference decoder barfs if it isn't).
---
Build system change only.


 configure|   2 +-
 libavcodec/Makefile  |   3 +-
 libavcodec/qsvenc_hevc.c | 190 +++
 3 files changed, 111 insertions(+), 84 deletions(-)

diff --git a/configure b/configure
index 70f33b64b..f290dd45d 100755
--- a/configure
+++ b/configure
@@ -2288,7 +2288,7 @@ h264_vaapi_encoder_deps="VAEncPictureParameterBufferH264"
 h264_vaapi_encoder_select="vaapi_encode cbs_h264 golomb"
 hevc_nvenc_encoder_deps="nvenc"
 hevc_qsv_decoder_select="hevc_mp4toannexb_bsf hevc_parser hevc_qsv_hwaccel 
qsvdec"
-hevc_qsv_encoder_select="hevc_ps qsvenc"
+hevc_qsv_encoder_select="qsvenc cbs_h265"
 hevc_vaapi_encoder_deps="VAEncPictureParameterBufferHEVC"
 hevc_vaapi_encoder_select="vaapi_encode cbs_h265 golomb"
 mjpeg_qsv_encoder_deps="libmfx"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ffd17c9cf..991275cac 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -283,8 +283,7 @@ OBJS-$(CONFIG_HEVC_DECODER)+= hevcdec.o 
hevc_mvs.o hevc_sei.o \
   hevcdsp.o hevc_filter.o 
h2645_parse.o hevc_data.o
 OBJS-$(CONFIG_HEVC_NVENC_ENCODER)  += nvenc_hevc.o
 OBJS-$(CONFIG_HEVC_QSV_DECODER)+= qsvdec_h2645.o
-OBJS-$(CONFIG_HEVC_QSV_ENCODER)+= qsvenc_hevc.o hevc_ps_enc.o   \
-  h2645_parse.o hevc_data.o
+OBJS-$(CONFIG_HEVC_QSV_ENCODER)+= qsvenc_hevc.o
 OBJS-$(CONFIG_HEVC_VAAPI_ENCODER)  += vaapi_encode_h265.o
 OBJS-$(CONFIG_HNM4_VIDEO_DECODER)  += hnm4video.o
 OBJS-$(CONFIG_HQ_HQA_DECODER)  += hq_hqa.o hq_hqadata.o hq_hqadsp.o \
diff --git a/libavcodec/qsvenc_hevc.c b/libavcodec/qsvenc_hevc.c
index 27ff0c6fe..adbb6c1b6 100644
--- a/libavcodec/qsvenc_hevc.c
+++ b/libavcodec/qsvenc_hevc.c
@@ -28,11 +28,11 @@
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
-#include "bytestream.h"
+#include "cbs.h"
+#include "cbs_h265.h"
 #include "get_bits.h"
 #include "hevc.h"
 #include "hevcdec.h"
-#include "h2645_parse.h"
 #include "internal.h"
 #include "qsv.h"
 #include "qsv_internal.h"
@@ -52,107 +52,135 @@ typedef struct QSVHEVCEncContext {
 
 static int generate_fake_vps(QSVEncContext *q, AVCodecContext *avctx)
 {
-GetByteContext gbc;
-PutByteContext pbc;
-
-GetBitContext gb;
-H2645NAL sps_nal = { NULL };
-HEVCSPS sps = { 0 };
-HEVCVPS vps = { 0 };
-uint8_t vps_buf[128], vps_rbsp_buf[128];
-uint8_t *new_extradata;
-unsigned int sps_id;
-int ret, i, type, vps_size;
+CodedBitstreamContext cbc;
+CodedBitstreamFragment ps;
+const H265RawSPS *sps;
+H265RawVPS vps;
+uint8_t *data = NULL;
+size_t data_size;
+int err, sps_pos, i;
 
 if (!avctx->extradata_size) {
-av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx\n");
+av_log(avctx, AV_LOG_ERROR,
+   "No parameter sets returned by libmfx.\n");
 return AVERROR_UNKNOWN;
 }
 
-/* parse the SPS */
-ret = ff_h2645_extract_rbsp(avctx->extradata + 4, avctx->extradata_size - 
4, _nal);
-if (ret < 0) {
-av_log(avctx, AV_LOG_ERROR, "Error unescaping the SPS buffer\n");
-return ret;
-}
+err = ff_cbs_init(, AV_CODEC_ID_HEVC, avctx);
+if (err < 0)
+return err;
 
-ret = init_get_bits8(, sps_nal.data, sps_nal.size);
-if (ret < 0) {
-av_freep(_nal.rbsp_buffer);
-return ret;
+err = ff_cbs_read(, , avctx->extradata, avctx->extradata_size);
+if (err < 0) {
+av_log(avctx, AV_LOG_ERROR,
+   "Error reading parameter sets returned by libmfx.\n");
+ff_cbs_close();
+return err;
 }
 
-get_bits(, 1);
-type = get_bits(, 6);
-if (type != HEVC_NAL_SPS) {
-av_log(avctx, AV_LOG_ERROR, "Unexpected NAL type in the extradata: 
%d\n",
-   type);
-av_freep(_nal.rbsp_buffer);
-return AVERROR_INVALIDDATA;
+sps = NULL;
+for (sps_pos = 0; sps_pos < ps.nb_units; sps_pos++) {
+if (ps.units[sps_pos].type == HEVC_NAL_SPS) {
+sps = ps.units[sps_pos].content;
+break;
+}
 }
-get_bits(, 9);
-
-ret = ff_hevc_parse_sps(, , _id, 0, NULL, avctx);
-av_freep(_nal.rbsp_buffer);
-if (ret < 0) {
-av_log(avctx, AV_LOG_ERROR, "Error parsing the SPS\n");
-return ret;
+if (!sps) {
+av_log(avctx, AV_LOG_ERROR, "No SPS returned by libmfx.\n");
+goto fail;
 }
 
-/* generate the VPS */
-vps.vps_max_layers = 1;
-vps.vps_max_sub_layers = sps.max_sub_layers;
-memcpy(, , sizeof(vps.ptl));
-vps.vps_sub_layer_ordering_info_present_flag = 1;
+   

[libav-devel] [PATCH 06/14] lavc: Add h264_redundant_pps bitstream filter

2017-08-10 Thread Mark Thompson
This applies a specific fixup to some Bluray streams which contain
redundant PPSs modifying irrelevant parameters of the stream which
confuse other transformations which require correct extradata.

A new single global PPS is created, and all of the redundant PPSs
within the stream are removed.
---
Unchanged.


 configure   |   1 +
 doc/bitstream_filters.texi  |   9 ++
 libavcodec/Makefile |   1 +
 libavcodec/bitstream_filters.c  |   1 +
 libavcodec/h264_redundant_pps_bsf.c | 176 
 5 files changed, 188 insertions(+)
 create mode 100644 libavcodec/h264_redundant_pps_bsf.c

diff --git a/configure b/configure
index f3d616800..bd4555dc8 100755
--- a/configure
+++ b/configure
@@ -2324,6 +2324,7 @@ vc1_parser_select="vc1dsp"
 # bitstream_filters
 aac_adtstoasc_bsf_select="adts_header"
 h264_metadata_bsf_select="cbs_h264"
+h264_redundant_pps_bsf_select="cbs_h264"
 mjpeg2jpeg_bsf_select="jpegtables"
 trace_headers_bsf_select="cbs_h264 cbs_h265"
 
diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 08f2f390c..e1de3035e 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -104,6 +104,15 @@ insert the string ``hello'' associated with the given UUID.
 
 @section h264_mp4toannexb
 
+@section h264_redundant_pps
+
+This applies a specific fixup to some Bluray streams which contain
+redundant PPSs modifying irrelevant parameters of the stream which
+confuse other transformations which require correct extradata.
+
+A new single global PPS is created, and all of the redundant PPSs
+within the stream are removed.
+
 @section imx_dump_header
 
 @section mjpeg2jpeg
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index c76dffe05..b37a3aa09 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -778,6 +778,7 @@ OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF)  += 
extract_extradata_bsf.o\
  h2645_parse.o
 OBJS-$(CONFIG_H264_METADATA_BSF)  += h264_metadata_bsf.o
 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)   += h264_mp4toannexb_bsf.o
+OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF) += h264_redundant_pps_bsf.o
 OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF)   += hevc_mp4toannexb_bsf.o
 OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF)+= imx_dump_header_bsf.o
 OBJS-$(CONFIG_MJPEG2JPEG_BSF) += mjpeg2jpeg_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 4ad50508c..8fd46a734 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -30,6 +30,7 @@ extern const AVBitStreamFilter ff_dump_extradata_bsf;
 extern const AVBitStreamFilter ff_extract_extradata_bsf;
 extern const AVBitStreamFilter ff_h264_metadata_bsf;
 extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf;
+extern const AVBitStreamFilter ff_h264_redundant_pps_bsf;
 extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_imx_dump_header_bsf;
 extern const AVBitStreamFilter ff_mjpeg2jpeg_bsf;
diff --git a/libavcodec/h264_redundant_pps_bsf.c 
b/libavcodec/h264_redundant_pps_bsf.c
new file mode 100644
index 0..b945daf36
--- /dev/null
+++ b/libavcodec/h264_redundant_pps_bsf.c
@@ -0,0 +1,176 @@
+/*
+ * 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 
+
+#include "libavutil/common.h"
+#include "libavutil/mem.h"
+
+#include "bsf.h"
+#include "cbs.h"
+#include "cbs_h264.h"
+#include "h264.h"
+
+
+typedef struct H264RedundantPPSContext {
+CodedBitstreamContext input;
+CodedBitstreamContext output;
+
+CodedBitstreamFragment access_unit;
+
+int global_pic_init_qp;
+int current_pic_init_qp;
+} H264RedundantPPSContext;
+
+
+static int h264_redundant_pps_fixup_pps(H264RedundantPPSContext *ctx,
+H264RawPPS *pps)
+{
+// Record the current value of pic_init_qp in order to fix up
+// following slices, then overwrite with the global value.
+ctx->current_pic_init_qp = pps->pic_init_qp_minus26 + 26;
+pps->pic_init_qp_minus26 = ctx->global_pic_init_qp - 26;
+
+// Some PPSs have this set, so it must be set in all of them.
+// (Slices which do not use such a PPS on input will still have
+// *_weight_l*flag as 

[libav-devel] [PATCH 09/14] vaapi_h264: Add support for AUD NAL units

2017-08-10 Thread Mark Thompson
Adds a new private option to enable them (off by default).
---
Unchanged.


 libavcodec/vaapi_encode_h264.c | 33 +
 1 file changed, 33 insertions(+)

diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c
index b48070c95..4f0e879e9 100644
--- a/libavcodec/vaapi_encode_h264.c
+++ b/libavcodec/vaapi_encode_h264.c
@@ -53,6 +53,7 @@ typedef struct VAAPIEncodeH264Context {
 int fixed_qp_p;
 int fixed_qp_b;
 
+H264RawAUD aud;
 H264RawSPS sps;
 H264RawPPS pps;
 H264RawSEI sei;
@@ -77,6 +78,7 @@ typedef struct VAAPIEncodeH264Context {
 
 CodedBitstreamContext cbc;
 CodedBitstreamFragment current_access_unit;
+int aud_needed;
 int sei_needed;
 } VAAPIEncodeH264Context;
 
@@ -84,6 +86,7 @@ typedef struct VAAPIEncodeH264Options {
 int qp;
 int quality;
 int low_power;
+int aud;
 int sei;
 } VAAPIEncodeH264Options;
 
@@ -143,6 +146,13 @@ static int 
vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
 CodedBitstreamFragment   *au = >current_access_unit;
 int err;
 
+if (priv->aud_needed) {
+err = vaapi_encode_h264_add_nal(avctx, au, >aud);
+if (err < 0)
+goto fail;
+priv->aud_needed = 0;
+}
+
 err = vaapi_encode_h264_add_nal(avctx, au, >sps);
 if (err < 0)
 goto fail;
@@ -167,6 +177,13 @@ static int 
vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
 CodedBitstreamFragment   *au = >current_access_unit;
 int err;
 
+if (priv->aud_needed) {
+err = vaapi_encode_h264_add_nal(avctx, au, >aud);
+if (err < 0)
+goto fail;
+priv->aud_needed = 0;
+}
+
 err = vaapi_encode_h264_add_nal(avctx, au, >slice);
 if (err < 0)
 goto fail;
@@ -189,6 +206,11 @@ static int 
vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
 int err, i;
 
 if (priv->sei_needed) {
+if (priv->aud_needed) {
+vaapi_encode_h264_add_nal(avctx, au, >aud);
+priv->aud_needed = 0;
+}
+
 memset(>sei, 0, sizeof(priv->sei));
 priv->sei.nal_unit_header.nal_unit_type = H264_NAL_SEI;
 
@@ -569,6 +591,14 @@ static int 
vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
 priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
 priv->dpb_delay = pic->display_order - pic->encode_order + 1;
 
+if (opt->aud) {
+priv->aud_needed = 1;
+priv->aud.nal_unit_header.nal_unit_type = H264_NAL_AUD;
+priv->aud.primary_pic_type = priv->primary_pic_type;
+} else {
+priv->aud_needed = 0;
+}
+
 if (opt->sei & SEI_IDENTIFIER && pic->encode_order == 0)
 priv->sei_needed = 1;
 
@@ -915,6 +945,9 @@ static const AVOption vaapi_encode_h264_options[] = {
   "on some platforms, does not support all features)",
   OFFSET(low_power), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
 
+{ "aud", "Include AUD",
+  OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
+
 { "sei", "Set SEI to include",
   OFFSET(sei), AV_OPT_TYPE_FLAGS,
   { .i64 = SEI_IDENTIFIER | SEI_TIMING },
-- 
2.11.0

___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

[libav-devel] [PATCH 02/14] lavc: Add coded bitstream read/write support for H.264

2017-08-10 Thread Mark Thompson
---
The golomb read/write functions are now local to cbs_h2645.c.


 configure|2 +
 libavcodec/Makefile  |1 +
 libavcodec/cbs.c |3 +
 libavcodec/cbs_h264.h|  427 +++
 libavcodec/cbs_h2645.c   |  997 ++
 libavcodec/cbs_h2645.h   |   43 ++
 libavcodec/cbs_h264_syntax.c | 1230 ++
 libavcodec/cbs_internal.h|3 +
 8 files changed, 2706 insertions(+)
 create mode 100644 libavcodec/cbs_h264.h
 create mode 100644 libavcodec/cbs_h2645.c
 create mode 100644 libavcodec/cbs_h2645.h
 create mode 100644 libavcodec/cbs_h264_syntax.c

diff --git a/configure b/configure
index befaed1e5..54af2e0cb 100755
--- a/configure
+++ b/configure
@@ -1740,6 +1740,7 @@ CONFIG_EXTRA="
 bswapdsp
 cabac
 cbs
+cbs_h264
 dirac_parse
 dvprofile
 faandct
@@ -1966,6 +1967,7 @@ w32threads_deps="atomics_native"
 threads_if_any="$THREADS_LIST"
 
 # subsystems
+cbs_h264_select="cbs golomb"
 dct_select="rdft"
 dirac_parse_select="golomb"
 error_resilience_select="me_cmp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 41da3ca7b..30fc388f8 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -54,6 +54,7 @@ OBJS-$(CONFIG_BLOCKDSP)+= blockdsp.o
 OBJS-$(CONFIG_BSWAPDSP)+= bswapdsp.o
 OBJS-$(CONFIG_CABAC)   += cabac.o
 OBJS-$(CONFIG_CBS) += cbs.o
+OBJS-$(CONFIG_CBS_H264)+= cbs_h2645.o h2645_parse.o
 OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o dct32_float.o
 OBJS-$(CONFIG_ERROR_RESILIENCE)+= error_resilience.o
 OBJS-$(CONFIG_FAANDCT) += faandct.o
diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index 4ff18a2e5..a1d274674 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -28,6 +28,9 @@
 
 
 static const CodedBitstreamType *cbs_type_table[] = {
+#if CONFIG_CBS_H264
+_cbs_type_h264,
+#endif
 };
 
 int ff_cbs_init(CodedBitstreamContext *ctx,
diff --git a/libavcodec/cbs_h264.h b/libavcodec/cbs_h264.h
new file mode 100644
index 0..b58f19f17
--- /dev/null
+++ b/libavcodec/cbs_h264.h
@@ -0,0 +1,427 @@
+/*
+ * 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
+ */
+
+#ifndef AVCODEC_CBS_H264_H
+#define AVCODEC_CBS_H264_H
+
+#include 
+#include 
+
+#include "cbs_h2645.h"
+#include "h264.h"
+
+
+enum {
+// This limit is arbitrary - it is sufficient for one message of each
+// type plus some repeats, and will therefore easily cover all sane
+// streams.  However, it is possible to make technically-valid streams
+// for which it will fail (for example, by including a large number of
+// user-data-unregistered messages).
+H264_MAX_SEI_PAYLOADS = 64,
+};
+
+
+typedef struct H264RawNALUnitHeader {
+uint8_t forbidden_zero_bit;
+uint8_t nal_ref_idc;
+uint8_t nal_unit_type;
+
+uint8_t svc_extension_flag;
+uint8_t avc_3d_extension_flag;
+} H264RawNALUnitHeader;
+
+typedef struct H264RawScalingList {
+int8_t delta_scale[64];
+} H264RawScalingList;
+
+typedef struct H264RawHRD {
+uint8_t cpb_cnt_minus1;
+uint8_t bit_rate_scale;
+uint8_t cpb_size_scale;
+
+uint32_t bit_rate_value_minus1[H264_MAX_CPB_CNT];
+uint32_t cpb_size_value_minus1[H264_MAX_CPB_CNT];
+uint8_t cbr_flag[H264_MAX_CPB_CNT];
+
+uint8_t initial_cpb_removal_delay_length_minus1;
+uint8_t cpb_removal_delay_length_minus1;
+uint8_t dpb_output_delay_length_minus1;
+uint8_t time_offset_length;
+} H264RawHRD;
+
+typedef struct H264RawVUI {
+uint8_t aspect_ratio_info_present_flag;
+uint8_t aspect_ratio_idc;
+uint16_t sar_width;
+uint16_t sar_height;
+
+uint8_t overscan_info_present_flag;
+uint8_t overscan_appropriate_flag;
+
+uint8_t video_signal_type_present_flag;
+uint8_t video_format;
+uint8_t video_full_range_flag;
+uint8_t colour_description_present_flag;
+uint8_t colour_primaries;
+uint8_t transfer_characteristics;
+uint8_t matrix_coefficients;
+
+uint8_t chroma_loc_info_present_flag;
+uint8_t chroma_sample_loc_type_top_field;
+uint8_t chroma_sample_loc_type_bottom_field;
+
+uint8_t timing_info_present_flag;
+

[libav-devel] [PATCH 05/14] lavc: Add h264_metadata bitstream filter

2017-08-10 Thread Mark Thompson
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.
---
The frame_rate option is renamed tick_rate to be more consistent with what is 
actually in the stream.

Crop unit has been changed to pixels rather than the subsample units used in 
the stream.


 configure  |   1 +
 doc/bitstream_filters.texi |  63 +
 libavcodec/Makefile|   1 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/h264_metadata_bsf.c | 511 +
 5 files changed, 577 insertions(+)
 create mode 100644 libavcodec/h264_metadata_bsf.c

diff --git a/configure b/configure
index 13dd7f54e..f3d616800 100755
--- a/configure
+++ b/configure
@@ -2323,6 +2323,7 @@ vc1_parser_select="vc1dsp"
 
 # bitstream_filters
 aac_adtstoasc_bsf_select="adts_header"
+h264_metadata_bsf_select="cbs_h264"
 mjpeg2jpeg_bsf_select="jpegtables"
 trace_headers_bsf_select="cbs_h264 cbs_h265"
 
diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 119a2267a..08f2f390c 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -39,6 +39,69 @@ When this option is enabled, the long-term headers are 
removed from the
 bitstream after extraction.
 @end table
 
+@section h264_metadata
+
+Modify metadata embedded in an H.264 stream.
+
+@table @option
+@item aud
+Insert or remove AUD NAL units in all access units of the stream.
+
+@table @samp
+@item insert
+@item remove
+@end table
+
+@item sample_aspect_ratio
+Set the sample aspect ratio of the stream in the VUI parameters.
+
+@item video_format
+@item video_full_range_flag
+Set the video format in the stream (see H.264 section E.2.1 and
+table E-2).
+
+@item colour_primaries
+@item transfer_characteristics
+@item matrix_coefficients
+Set the colour description in the stream (see H.264 section E.2.1
+and tables E-3, E-4 and E-5).
+
+@item chroma_sample_loc_type
+Set the chroma sample location in the stream (see H.264 section
+E.2.1 and figure E-1).
+
+@item tick_rate
+Set the tick rate (num_units_in_tick / time_scale) in the VUI
+parameters.  This is the smallest time unit representable in the
+stream, and in many cases represents the field rate of the stream
+(double the frame rate).
+@item fixed_frame_rate_flag
+Set whether the stream has fixed framerate - typically this indicates
+that the framerate is exactly half the tick rate, but the exact
+meaning is dependent on interlacing and the picture structure (see
+H.264 section E.2.1 and table E-6).
+
+@item crop_left
+@item crop_right
+@item crop_top
+@item crop_bottom
+Set the frame cropping offsets in the SPS.  These values will replace
+the current ones if the stream is already cropped.
+
+These fields are set in pixels.  Note that some sizes may not be
+representable if the chroma is subsampled or the stream is interlaced
+(see H.264 section 7.4.2.1.1).
+
+@item sei_user_data
+Insert a string as SEI unregistered user data.  The argument must
+be of the form @emph{UUID+string}, where the UUID is as hex digits
+possibly separated by hyphens, and the string can be anything.
+
+For example, @samp{086f3693-b7b3-4f2c-9653-21492feee5b8+hello} will
+insert the string ``hello'' associated with the given UUID.
+
+@end table
+
 @section h264_mp4toannexb
 
 @section imx_dump_header
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 09772a85f..c76dffe05 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -776,6 +776,7 @@ OBJS-$(CONFIG_CHOMP_BSF)  += chomp_bsf.o
 OBJS-$(CONFIG_DUMP_EXTRADATA_BSF) += dump_extradata_bsf.o
 OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF)  += extract_extradata_bsf.o\
  h2645_parse.o
+OBJS-$(CONFIG_H264_METADATA_BSF)  += h264_metadata_bsf.o
 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)   += h264_mp4toannexb_bsf.o
 OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF)   += hevc_mp4toannexb_bsf.o
 OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF)+= imx_dump_header_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 2e423acaf..4ad50508c 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -28,6 +28,7 @@ extern const AVBitStreamFilter ff_aac_adtstoasc_bsf;
 extern const AVBitStreamFilter ff_chomp_bsf;
 extern const AVBitStreamFilter ff_dump_extradata_bsf;
 extern const AVBitStreamFilter ff_extract_extradata_bsf;
+extern const AVBitStreamFilter ff_h264_metadata_bsf;
 extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_imx_dump_header_bsf;
diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c
new file mode 100644
index 0..25145d676
--- /dev/null
+++ b/libavcodec/h264_metadata_bsf.c
@@ -0,0 +1,511 @@
+/*
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it 

[libav-devel] [PATCH 03/14] lavc: Add coded bitstream read/write support for H.265

2017-08-10 Thread Mark Thompson
---
Build system changes only.


 configure|2 +
 libavcodec/Makefile  |1 +
 libavcodec/cbs.c |3 +
 libavcodec/cbs_h2645.c   |  410 +++-
 libavcodec/cbs_h265.h|  537 +++
 libavcodec/cbs_h265_syntax.c | 1502 ++
 libavcodec/cbs_internal.h|1 +
 7 files changed, 2453 insertions(+), 3 deletions(-)
 create mode 100644 libavcodec/cbs_h265.h
 create mode 100644 libavcodec/cbs_h265_syntax.c

diff --git a/configure b/configure
index 54af2e0cb..535862fca 100755
--- a/configure
+++ b/configure
@@ -1741,6 +1741,7 @@ CONFIG_EXTRA="
 cabac
 cbs
 cbs_h264
+cbs_h265
 dirac_parse
 dvprofile
 faandct
@@ -1968,6 +1969,7 @@ threads_if_any="$THREADS_LIST"
 
 # subsystems
 cbs_h264_select="cbs golomb"
+cbs_h265_select="cbs golomb"
 dct_select="rdft"
 dirac_parse_select="golomb"
 error_resilience_select="me_cmp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 30fc388f8..9791fb342 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -55,6 +55,7 @@ OBJS-$(CONFIG_BSWAPDSP)+= bswapdsp.o
 OBJS-$(CONFIG_CABAC)   += cabac.o
 OBJS-$(CONFIG_CBS) += cbs.o
 OBJS-$(CONFIG_CBS_H264)+= cbs_h2645.o h2645_parse.o
+OBJS-$(CONFIG_CBS_H265)+= cbs_h2645.o h2645_parse.o
 OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o dct32_float.o
 OBJS-$(CONFIG_ERROR_RESILIENCE)+= error_resilience.o
 OBJS-$(CONFIG_FAANDCT) += faandct.o
diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index a1d274674..505cfb7ed 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -31,6 +31,9 @@ static const CodedBitstreamType *cbs_type_table[] = {
 #if CONFIG_CBS_H264
 _cbs_type_h264,
 #endif
+#if CONFIG_CBS_H265
+_cbs_type_h265,
+#endif
 };
 
 int ff_cbs_init(CodedBitstreamContext *ctx,
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 191e0e90d..3261a3bdd 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -23,10 +23,12 @@
 #include "cbs.h"
 #include "cbs_internal.h"
 #include "cbs_h264.h"
+#include "cbs_h265.h"
 #include "golomb.h"
 #include "h264.h"
 #include "h264_sei.h"
 #include "h2645_parse.h"
+#include "hevc.h"
 
 
 static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, BitstreamContext *bc,
@@ -237,6 +239,7 @@ static int cbs_write_se_golomb(CodedBitstreamContext *ctx, 
PutBitContext *pbc,
 
 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
 #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
+#define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
 
 
 #define READ
@@ -299,6 +302,10 @@ static int cbs_h2645_read_more_rbsp_data(BitstreamContext 
*bc)
 #include "cbs_h264_syntax.c"
 #undef FUNC
 
+#define FUNC(name) FUNC_H265(READWRITE, name)
+#include "cbs_h265_syntax.c"
+#undef FUNC
+
 #undef READ
 #undef READWRITE
 #undef RWContext
@@ -368,6 +375,10 @@ static int cbs_h2645_read_more_rbsp_data(BitstreamContext 
*bc)
 #include "cbs_h264_syntax.c"
 #undef FUNC
 
+#define FUNC(name) FUNC_H265(READWRITE, name)
+#include "cbs_h265_syntax.c"
+#undef FUNC
+
 #undef WRITE
 #undef READWRITE
 #undef RWContext
@@ -428,6 +439,40 @@ static void cbs_h264_free_nal_unit(CodedBitstreamUnit 
*unit)
 av_freep(>content);
 }
 
+static void cbs_h265_free_nal_unit(CodedBitstreamUnit *unit)
+{
+switch (unit->type) {
+case HEVC_NAL_VPS:
+av_freep(&((H265RawVPS*)unit->content)->extension_data.data);
+break;
+case HEVC_NAL_SPS:
+av_freep(&((H265RawSPS*)unit->content)->extension_data.data);
+break;
+case HEVC_NAL_PPS:
+av_freep(&((H265RawPPS*)unit->content)->extension_data.data);
+break;
+case HEVC_NAL_TRAIL_N:
+case HEVC_NAL_TRAIL_R:
+case HEVC_NAL_TSA_N:
+case HEVC_NAL_TSA_R:
+case HEVC_NAL_STSA_N:
+case HEVC_NAL_STSA_R:
+case HEVC_NAL_RADL_N:
+case HEVC_NAL_RADL_R:
+case HEVC_NAL_RASL_N:
+case HEVC_NAL_RASL_R:
+case HEVC_NAL_BLA_W_LP:
+case HEVC_NAL_BLA_W_RADL:
+case HEVC_NAL_BLA_N_LP:
+case HEVC_NAL_IDR_W_RADL:
+case HEVC_NAL_IDR_N_LP:
+case HEVC_NAL_CRA_NUT:
+av_freep(&((H265RawSlice*)unit->content)->data);
+break;
+}
+av_freep(>content);
+}
+
 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
const H2645Packet *packet)
@@ -542,6 +587,58 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext 
*ctx,
"header.\n", bytestream2_get_bytes_left());
 }
 
+} else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
+// HVCC header.
+size_t size, start, end;
+int i, j, nb_arrays, nal_unit_type, nb_nals, version;
+
+priv->mp4 = 1;
+
+bytestream2_init(, frag->data, 

[libav-devel] [PATCH 04/14] lavc: Add trace_headers bitstream filter

2017-08-10 Thread Mark Thompson
Supports all streams that the coded bitstream infrastructure does
(currently H.264 and H.265).
---
Removed MPEG-2 from this patch; will add it again later when it is complete.


 configure  |   1 +
 doc/bitstream_filters.texi |   8 +++
 libavcodec/Makefile|   1 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/trace_headers_bsf.c | 125 +
 5 files changed, 136 insertions(+)
 create mode 100644 libavcodec/trace_headers_bsf.c

diff --git a/configure b/configure
index 535862fca..13dd7f54e 100755
--- a/configure
+++ b/configure
@@ -2324,6 +2324,7 @@ vc1_parser_select="vc1dsp"
 # bitstream_filters
 aac_adtstoasc_bsf_select="adts_header"
 mjpeg2jpeg_bsf_select="jpegtables"
+trace_headers_bsf_select="cbs_h264 cbs_h265"
 
 # external libraries
 avisynth_deps="LoadLibrary"
diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 64f91f4b5..119a2267a 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -95,6 +95,14 @@ This bitstream filter passes the packets through unchanged.
 
 @section remove_extradata
 
+@section trace_headers
+
+Log trace output containing all syntax elements in the coded stream
+headers (everything above the level of individual coded blocks).
+This can be useful for debugging low-level stream issues.
+
+Supports H.264 and H.265.
+
 @section vp9_superframe
 
 Combine VP9 frames into superframes.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 9791fb342..09772a85f 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -786,6 +786,7 @@ OBJS-$(CONFIG_NOISE_BSF)  += noise_bsf.o
 OBJS-$(CONFIG_NULL_BSF)   += null_bsf.o
 OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF)   += remove_extradata_bsf.o
 OBJS-$(CONFIG_TEXT2MOVSUB_BSF)+= movsub_bsf.o
+OBJS-$(CONFIG_TRACE_HEADERS_BSF)  += trace_headers_bsf.o
 OBJS-$(CONFIG_VP9_RAW_REORDER_BSF)+= vp9_raw_reorder_bsf.o
 OBJS-$(CONFIG_VP9_SUPERFRAME_BSF) += vp9_superframe_bsf.o
 OBJS-$(CONFIG_VP9_SUPERFRAME_SPLIT_BSF)   += vp9_superframe_split_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 79ce40f9e..2e423acaf 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -38,6 +38,7 @@ extern const AVBitStreamFilter ff_null_bsf;
 extern const AVBitStreamFilter ff_text2movsub_bsf;
 extern const AVBitStreamFilter ff_noise_bsf;
 extern const AVBitStreamFilter ff_remove_extradata_bsf;
+extern const AVBitStreamFilter ff_trace_headers_bsf;
 extern const AVBitStreamFilter ff_vp9_raw_reorder_bsf;
 extern const AVBitStreamFilter ff_vp9_superframe_bsf;
 extern const AVBitStreamFilter ff_vp9_superframe_split_bsf;
diff --git a/libavcodec/trace_headers_bsf.c b/libavcodec/trace_headers_bsf.c
new file mode 100644
index 0..2d1fe636c
--- /dev/null
+++ b/libavcodec/trace_headers_bsf.c
@@ -0,0 +1,125 @@
+/*
+ * 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 
+
+#include "libavutil/avstring.h"
+#include "libavutil/common.h"
+#include "libavutil/log.h"
+
+#include "bsf.h"
+#include "cbs.h"
+
+
+typedef struct TraceHeadersContext {
+CodedBitstreamContext cbc;
+} TraceHeadersContext;
+
+
+static int trace_headers_init(AVBSFContext *bsf)
+{
+TraceHeadersContext *ctx = bsf->priv_data;
+int err;
+
+err = ff_cbs_init(>cbc, bsf->par_in->codec_id, bsf);
+if (err < 0)
+return err;
+
+ctx->cbc.trace_enable = 1;
+ctx->cbc.trace_level  = AV_LOG_INFO;
+
+if (bsf->par_in->extradata) {
+CodedBitstreamFragment ps;
+
+av_log(bsf, AV_LOG_INFO, "Extradata\n");
+
+err = ff_cbs_read_extradata(>cbc, , bsf->par_in);
+if (err < 0) {
+av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
+return err;
+}
+
+ff_cbs_fragment_uninit(>cbc, );
+}
+
+return 0;
+}
+
+static void trace_headers_close(AVBSFContext *bsf)
+{
+TraceHeadersContext *ctx = bsf->priv_data;
+
+ff_cbs_close(>cbc);
+}
+
+static int trace_headers(AVBSFContext *bsf, AVPacket *out)
+{
+TraceHeadersContext *ctx = bsf->priv_data;
+CodedBitstreamFragment au;
+AVPacket *in;
+char tmp[256] = { 0 };
+int err;
+
+

[libav-devel] [PATCH 07/14] lavc: Add hevc_metadata bitstream filter

2017-08-10 Thread Mark Thompson
This is able to modify some header metadata found in the VPS/SPS/VUI,
and can also add/remove AUDs.
---
Crop unit has been changed to pixels rather than the subsample units used in 
the stream.

Fixed updating the VPS in extradata and some failure cases.


 configure  |   1 +
 doc/bitstream_filters.texi |  54 +
 libavcodec/Makefile|   1 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/h265_metadata_bsf.c | 458 +
 5 files changed, 515 insertions(+)
 create mode 100644 libavcodec/h265_metadata_bsf.c

diff --git a/configure b/configure
index bd4555dc8..dfa3867ac 100755
--- a/configure
+++ b/configure
@@ -2325,6 +2325,7 @@ vc1_parser_select="vc1dsp"
 aac_adtstoasc_bsf_select="adts_header"
 h264_metadata_bsf_select="cbs_h264"
 h264_redundant_pps_bsf_select="cbs_h264"
+hevc_metadata_bsf_select="cbs_h265"
 mjpeg2jpeg_bsf_select="jpegtables"
 trace_headers_bsf_select="cbs_h264 cbs_h265"
 
diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index e1de3035e..21371ea9a 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -113,6 +113,60 @@ confuse other transformations which require correct 
extradata.
 A new single global PPS is created, and all of the redundant PPSs
 within the stream are removed.
 
+@section hevc_metadata
+
+Modify metadata embedded in an HEVC stream.
+
+@table @option
+@item aud
+Insert or remove AUD NAL units in all access units of the stream.
+
+@table @samp
+@item insert
+@item remove
+@end table
+
+@item sample_aspect_ratio
+Set the sample aspect ratio in the stream in the VUI parameters.
+
+@item video_format
+@item video_full_range_flag
+Set the video format in the stream (see H.265 section E.3.1 and
+table E.2).
+
+@item colour_primaries
+@item transfer_characteristics
+@item matrix_coefficients
+Set the colour description in the stream (see H.265 section E.3.1
+and tables E.3, E.4 and E.5).
+
+@item chroma_sample_loc_type
+Set the chroma sample location in the stream (see H.265 section
+E.3.1 and figure E.1).
+
+@item tick_rate
+Set the tick rate in the VPS and VUI parameters (num_units_in_tick /
+time_scale).  Combined with @option{num_ticks_poc_diff_one}, this can
+set a constant framerate in the stream.  Note that it is likely to be
+overridden by container parameters when the stream is in a container.
+
+@item num_ticks_poc_diff_one
+Set poc_proportional_to_timing_flag in VPS and VUI and use this value
+to set num_ticks_poc_diff_one_minus1 (see H.265 sections 7.4.3.1 and
+E.3.1).  Ignored if @option{tick_rate} is not also set.
+
+@item crop_left
+@item crop_right
+@item crop_top
+@item crop_bottom
+Set the conformance window cropping offsets in the SPS.  These values
+will replace the current ones if the stream is already cropped.
+
+These fields are set in pixels.  Note that some sizes may not be
+representable if the chroma is subsampled (H.265 section 7.4.3.2.1).
+
+@end table
+
 @section imx_dump_header
 
 @section mjpeg2jpeg
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index b37a3aa09..e1ae1e451 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -779,6 +779,7 @@ OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF)  += 
extract_extradata_bsf.o\
 OBJS-$(CONFIG_H264_METADATA_BSF)  += h264_metadata_bsf.o
 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)   += h264_mp4toannexb_bsf.o
 OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF) += h264_redundant_pps_bsf.o
+OBJS-$(CONFIG_HEVC_METADATA_BSF)  += h265_metadata_bsf.o
 OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF)   += hevc_mp4toannexb_bsf.o
 OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF)+= imx_dump_header_bsf.o
 OBJS-$(CONFIG_MJPEG2JPEG_BSF) += mjpeg2jpeg_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 8fd46a734..e90919f60 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -31,6 +31,7 @@ extern const AVBitStreamFilter ff_extract_extradata_bsf;
 extern const AVBitStreamFilter ff_h264_metadata_bsf;
 extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_h264_redundant_pps_bsf;
+extern const AVBitStreamFilter ff_hevc_metadata_bsf;
 extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_imx_dump_header_bsf;
 extern const AVBitStreamFilter ff_mjpeg2jpeg_bsf;
diff --git a/libavcodec/h265_metadata_bsf.c b/libavcodec/h265_metadata_bsf.c
new file mode 100644
index 0..aef4a55fb
--- /dev/null
+++ b/libavcodec/h265_metadata_bsf.c
@@ -0,0 +1,458 @@
+/*
+ * 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 

[libav-devel] [PATCH 00/14] Coded bitstream editing (v6)

2017-08-10 Thread Mark Thompson
The main change here is to make cbs and cbs_codec subsystems for configure, 
plus miscellaneous minor fixes reflecting review comments.

This is H.264/5 only to make it a bit more cohesive.  I have some further 
changes to make to MPEG-2, and that can follow later.

The build system changes want some review, but after that hopefully 1-9 are 
done.  10-14 are as yet unreviewed.

Thanks,

- Mark
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

[libav-devel] [PATCH 01/14] lavc: Add coded bitstream read/write API

2017-08-10 Thread Mark Thompson
---
The golomb read/write functions have moved to the following patch to make the 
dependencies work out sensibly.


 configure |   1 +
 libavcodec/Makefile   |   1 +
 libavcodec/cbs.c  | 461 ++
 libavcodec/cbs.h  | 274 +++
 libavcodec/cbs_internal.h |  83 +
 5 files changed, 820 insertions(+)
 create mode 100644 libavcodec/cbs.c
 create mode 100644 libavcodec/cbs.h
 create mode 100644 libavcodec/cbs_internal.h

diff --git a/configure b/configure
index 4510100f3..befaed1e5 100755
--- a/configure
+++ b/configure
@@ -1739,6 +1739,7 @@ CONFIG_EXTRA="
 blockdsp
 bswapdsp
 cabac
+cbs
 dirac_parse
 dvprofile
 faandct
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index bb568ddbe..41da3ca7b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -53,6 +53,7 @@ OBJS-$(CONFIG_AUDIODSP)+= audiodsp.o
 OBJS-$(CONFIG_BLOCKDSP)+= blockdsp.o
 OBJS-$(CONFIG_BSWAPDSP)+= bswapdsp.o
 OBJS-$(CONFIG_CABAC)   += cabac.o
+OBJS-$(CONFIG_CBS) += cbs.o
 OBJS-$(CONFIG_DCT) += dct.o dct32_fixed.o dct32_float.o
 OBJS-$(CONFIG_ERROR_RESILIENCE)+= error_resilience.o
 OBJS-$(CONFIG_FAANDCT) += faandct.o
diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
new file mode 100644
index 0..4ff18a2e5
--- /dev/null
+++ b/libavcodec/cbs.c
@@ -0,0 +1,461 @@
+/*
+ * 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 
+
+#include "config.h"
+
+#include "libavutil/avassert.h"
+#include "libavutil/common.h"
+
+#include "cbs.h"
+#include "cbs_internal.h"
+
+
+static const CodedBitstreamType *cbs_type_table[] = {
+};
+
+int ff_cbs_init(CodedBitstreamContext *ctx,
+enum AVCodecID codec_id, void *log_ctx)
+{
+const CodedBitstreamType *type;
+int i;
+
+type = NULL;
+for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
+if (cbs_type_table[i]->codec_id == codec_id) {
+type = cbs_type_table[i];
+break;
+}
+}
+if (!type)
+return AVERROR(EINVAL);
+
+ctx->log_ctx = log_ctx;
+
+ctx->codec = type;
+
+ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
+if (!ctx->priv_data)
+return AVERROR(ENOMEM);
+
+ctx->decompose_unit_types = NULL;
+
+ctx->trace_enable = 0;
+ctx->trace_level  = AV_LOG_TRACE;
+
+return 0;
+}
+
+void ff_cbs_close(CodedBitstreamContext *ctx)
+{
+if (ctx->codec && ctx->codec->close)
+ctx->codec->close(ctx);
+
+av_freep(>priv_data);
+}
+
+static void cbs_unit_uninit(CodedBitstreamContext *ctx,
+CodedBitstreamUnit *unit)
+{
+if (ctx->codec->free_unit && unit->content && !unit->content_external)
+ctx->codec->free_unit(unit);
+
+av_freep(>data);
+unit->data_size = 0;
+unit->data_bit_padding = 0;
+}
+
+void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
+CodedBitstreamFragment *frag)
+{
+int i;
+
+for (i = 0; i < frag->nb_units; i++)
+cbs_unit_uninit(ctx, >units[i]);
+av_freep(>units);
+frag->nb_units = 0;
+
+av_freep(>data);
+frag->data_size = 0;
+frag->data_bit_padding = 0;
+}
+
+static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
+ CodedBitstreamFragment *frag)
+{
+int err, i, j;
+
+for (i = 0; i < frag->nb_units; i++) {
+if (ctx->decompose_unit_types) {
+for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
+if (ctx->decompose_unit_types[j] == frag->units[i].type)
+break;
+}
+if (j >= ctx->nb_decompose_unit_types)
+continue;
+}
+
+err = ctx->codec->read_unit(ctx, >units[i]);
+if (err == AVERROR(ENOSYS)) {
+av_log(ctx->log_ctx, AV_LOG_WARNING,
+   "Decomposition unimplemented for unit %d "
+   "(type %d).\n", i, frag->units[i].type);
+} else if (err < 0) {
+av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
+   "(type %d).\n", 

Re: [libav-devel] [PATCH] h264: Add support for alternative transfer characterics SEI

2017-08-10 Thread Luca Barbato
On 08/08/2017 16:06, Vittorio Giovara wrote:
> The use of this SEI is for backward compatibility in HLG HDR systems:
> older devices that cannot interpret the "arib-std-b67" transfer will
> get the compatible transfer (usually bt709 or bt2020) from the VUI,
> while newer devices that can interpret HDR will read the SEI and use
> its value instead.
> 

Looks good!

lu

___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Re: [libav-devel] [PATCH 1/2 V5] libavfilter/overlay_qsv: Add QSV overlay vpp filter

2017-08-10 Thread Anton Khirnov
Quoting Huang, Zhengxu (2017-08-10 03:35:09)

>From 873222a2fa0c0686e0611bc769707947fe243d27 Mon Sep 17 00:00:00 2001
>+static int eval_expr(AVFilterContext *ctx)
>+{
>+QSVOverlayContext *vpp = ctx->priv;
>+double *var_values = vpp->var_values;
>+intret = 0;
>+AVExpr *ox_expr, *oy_expr, *ow_expr, *oh_expr;

Those need to be initialized to NULL, otherwise you have invalid frees
in the failure path.

Otherwise looks ok.

-- 
Anton Khirnov
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Re: [libav-devel] [PATCH 13/22] vaapi_h264: Add support for AUD NAL units

2017-08-10 Thread Anton Khirnov
Quoting Mark Thompson (2017-07-29 23:16:36)
> Adds a new private option to enable them (off by default).
> ---
>  libavcodec/vaapi_encode_h264.c | 33 +
>  1 file changed, 33 insertions(+)

Looks ok

-- 
Anton Khirnov
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Re: [libav-devel] [PATCH 12/22] vaapi_h264: Convert to use coded bitstream infrastructure

2017-08-10 Thread Anton Khirnov
Quoting Mark Thompson (2017-07-29 23:16:35)
> ---
>  libavcodec/Makefile|2 +-
>  libavcodec/vaapi_encode_h264.c | 1421 
> 
>  2 files changed, 548 insertions(+), 875 deletions(-)
> 
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 0d8d5eb44..d1fd6fae2 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -272,7 +272,7 @@ OBJS-$(CONFIG_H264_NVENC_ENCODER)  += nvenc_h264.o
>  OBJS-$(CONFIG_H264_OMX_ENCODER)+= omx.o
>  OBJS-$(CONFIG_H264_QSV_DECODER)+= qsvdec_h2645.o
>  OBJS-$(CONFIG_H264_QSV_ENCODER)+= qsvenc_h264.o
> -OBJS-$(CONFIG_H264_VAAPI_ENCODER)  += vaapi_encode_h264.o 
> vaapi_encode_h26x.o
> +OBJS-$(CONFIG_H264_VAAPI_ENCODER)  += vaapi_encode_h264.o

I'd expect a dependency on cbs here (or in configure as discussed on
IRC).

The rest looks sane

-- 
Anton Khirnov
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Re: [libav-devel] [PATCH 11/22] lavc: Add hevc_metadata bitstream filter

2017-08-10 Thread Anton Khirnov
Quoting Mark Thompson (2017-07-29 23:16:34)
> This is able to modify some header metadata found in the VPS/SPS/VUI,
> and can also add/remove AUDs.
> ---
>  doc/bitstream_filters.texi |  54 ++
>  libavcodec/Makefile|   2 +
>  libavcodec/bitstream_filters.c |   1 +
>  libavcodec/h265_metadata_bsf.c | 429 
> +
>  4 files changed, 486 insertions(+)
>  create mode 100644 libavcodec/h265_metadata_bsf.c
> 
> diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
> index f121d4757..834cf07f6 100644
> --- a/doc/bitstream_filters.texi
> +++ b/doc/bitstream_filters.texi
> @@ -107,6 +107,60 @@ confuse other transformations which require correct 
> extradata.
>  A new single global PPS is created, and all of the redundant PPSs
>  within the stream are removed.
>  
> +@section hevc_metadata
> +
> +Modify metadata embedded in an HEVC stream.
> +
> +@table @option
> +@item aud
> +Insert or remove AUD NAL units in all access units of the stream.
> +
> +@table @samp
> +@item insert
> +@item remove
> +@end table
> +
> +@item sample_aspect_ratio
> +Set the sample aspect ratio in the stream in the VUI parameters.
> +
> +@item video_format
> +@item video_full_range_flag
> +Set the video format in the stream (see H.265 section E.3.1 and
> +table E.2).
> +
> +@item colour_primaries
> +@item transfer_characteristics
> +@item matrix_coefficients
> +Set the colour description in the stream (see H.265 section E.3.1
> +and tables E.3, E.4 and E.5).
> +
> +@item chroma_sample_loc_type
> +Set the chroma sample location in the stream (see H.265 section
> +E.3.1 and figure E.1).
> +
> +@item tick_rate
> +Set the tick rate in the VPS and VUI parameters (num_units_in_tick /
> +time_scale).  Combined with @option{num_ticks_poc_diff_one}, this can
> +set a constant framerate in the stream.  Note that it is likely to be
> +overridden by container parameters when the stream is in a container.
> +
> +@item num_ticks_poc_diff_one
> +Set poc_proportional_to_timing_flag in VPS and VUI and use this value
> +to set num_ticks_poc_diff_one_minus1 (see H.265 sections 7.4.3.1 and
> +E.3.1).  Ignored if @option{tick_rate} is not also set.
> +
> +@item crop_left
> +@item crop_right
> +@item crop_top
> +@item crop_bottom
> +Set the conformance window cropping offsets in the SPS.  These values
> +will replace the current ones if the stream is already cropped.
> +
> +The unit of these values is chroma pixels after subsampling (see
> +H.265 section 7.4.3.2.1).

I suppose this should be updated in line with the h264 one.

Otherwise looks ok.

-- 
Anton Khirnov
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Re: [libav-devel] [PATCH 07/22] lavc: Add coded bitstream read/write support for MPEG-2

2017-08-10 Thread Anton Khirnov
Quoting Mark Thompson (2017-07-29 23:16:30)
> ---
>  libavcodec/cbs.c  |   1 +
>  libavcodec/cbs_internal.h |   1 +
>  libavcodec/cbs_mpeg2.c| 355 
> ++
>  libavcodec/cbs_mpeg2.h| 208 +
>  libavcodec/cbs_mpeg2_syntax.c | 340 
>  5 files changed, 905 insertions(+)
>  create mode 100644 libavcodec/cbs_mpeg2.c
>  create mode 100644 libavcodec/cbs_mpeg2.h
>  create mode 100644 libavcodec/cbs_mpeg2_syntax.c
> 
> diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
> index 6030cfd48..2b98b519b 100644
> --- a/libavcodec/cbs.c
> +++ b/libavcodec/cbs.c
> @@ -29,6 +29,7 @@
>  static const CodedBitstreamType *cbs_type_table[] = {
>  _cbs_type_h264,
>  _cbs_type_h265,
> +_cbs_type_mpeg2,
>  };
>  
>  int ff_cbs_init(CodedBitstreamContext *ctx,
> diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h
> index 46d41db6d..57c7f1e36 100644
> --- a/libavcodec/cbs_internal.h
> +++ b/libavcodec/cbs_internal.h
> @@ -98,6 +98,7 @@ int ff_cbs_write_se_golomb(CodedBitstreamContext *ctx, 
> PutBitContext *pbc,
>  
>  extern const CodedBitstreamType ff_cbs_type_h264;
>  extern const CodedBitstreamType ff_cbs_type_h265;
> +extern const CodedBitstreamType ff_cbs_type_mpeg2;
>  
>  
>  #endif /* AVCODEC_CBS_INTERNAL_H */
> diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
> new file mode 100644
> index 0..ef54311ae
> --- /dev/null
> +++ b/libavcodec/cbs_mpeg2.c
> @@ -0,0 +1,355 @@
> +/*
> + * 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/avassert.h"
> +
> +#include "cbs.h"
> +#include "cbs_internal.h"
> +#include "cbs_mpeg2.h"
> +#include "internal.h"
> +
> +
> +#define HEADER(name) do { \
> +ff_cbs_trace_header(ctx, name); \
> +} while (0)
> +
> +#define CHECK(call) do { \
> +err = (call); \
> +if (err < 0) \
> +return err; \
> +} while (0)
> +
> +#define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
> +#define FUNC_MPEG2(rw, name) FUNC_NAME(rw, mpeg2, name)
> +#define FUNC(name) FUNC_MPEG2(READWRITE, name)
> +
> +
> +#define READ
> +#define READWRITE read
> +#define RWContext BitstreamContext
> +
> +#define xui(width, name, var) do { \
> +uint32_t value = 0; \
> +CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
> +   , 0, (1 << width) - 1)); \
> +var = value; \
> +} while (0)
> +
> +#define ui(width, name) \
> +xui(width, name, current->name)
> +
> +#define marker_bit() do { \
> +av_unused int one = 1; \
> +CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", , 1, 1)); \
> +} while (0)
> +
> +#define nextbits(width, compare, var) (var = bitstream_peek(rw, width), \
> +   var == (compare))
> +
> +#include "cbs_mpeg2_syntax.c"
> +
> +#undef READ
> +#undef READWRITE
> +#undef RWContext
> +#undef xui
> +#undef ui
> +#undef marker_bit
> +#undef nextbits
> +
> +
> +#define WRITE
> +#define READWRITE write
> +#define RWContext PutBitContext
> +
> +#define xui(width, name, var) do { \
> +CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
> +var, 0, (1 << width) - 1)); \
> +} while (0)
> +
> +#define ui(width, name) \
> +xui(width, name, current->name)
> +
> +#define marker_bit() do { \
> +CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", 1, 1, 1)); \
> +} while (0)
> +
> +#define nextbits(width, compare, var) (var)
> +
> +#include "cbs_mpeg2_syntax.c"
> +
> +#undef READ
> +#undef READWRITE
> +#undef RWContext
> +#undef xui
> +#undef ui
> +#undef marker_bit
> +#undef nextbits
> +
> +
> +static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
> +CodedBitstreamFragment *frag,
> +int header)
> +{
> +const uint8_t *start, *end;
> +uint8_t *unit_data;
> +uint32_t start_code = -1, next_start_code = -1;
> +size_t unit_size;
> +int err, i, unit_type;
> +
> +if (frag->nb_units != 0)
> +return AVERROR(EINVAL);
> +
> +start =