[libav-devel] [PATCH 02/15] hevc: Improve stream constraint values in common header

2017-06-23 Thread Mark Thompson
Add comments to describe the sources of the constraint values expressed here,
and add some more related values which will be used in following patches.

Fix the incorrect values for SPS and PPS count (they are not the same as those
used for H.264), and remove HEVC_MAX_CU_SIZE because it is not used anywhere.
---
 libavcodec/hevc.h| 57 +---
 libavcodec/hevc_ps.c |  2 +-
 libavcodec/hevc_ps.h |  6 +++---
 libavformat/hevc.c   |  6 +++---
 4 files changed, 52 insertions(+), 19 deletions(-)

diff --git a/libavcodec/hevc.h b/libavcodec/hevc.h
index 9d956d083..2d5f4569d 100644
--- a/libavcodec/hevc.h
+++ b/libavcodec/hevc.h
@@ -74,19 +74,52 @@ enum HEVCSliceType {
 HEVC_SLICE_I = 2,
 };
 
-/**
- * 7.4.2.1
- */
-#define HEVC_MAX_SUB_LAYERS 7
-#define HEVC_MAX_VPS_COUNT 16
-#define HEVC_MAX_SPS_COUNT 32
-#define HEVC_MAX_PPS_COUNT 256
-#define HEVC_MAX_SHORT_TERM_RPS_COUNT 64
-#define HEVC_MAX_CU_SIZE 128
+enum {
+// 7.4.3.1: vps_max_layers_minus1 is in [0, 62].
+HEVC_MAX_LAYERS = 63,
+// 7.4.3.1: vps_max_sub_layers_minus1 is in [0, 6].
+HEVC_MAX_SUB_LAYERS = 7,
+// 7.4.3.1: vps_num_layer_sets_minus1 is in [0, 1023].
+HEVC_MAX_LAYER_SETS = 1024,
+
+// 7.4.2.1: vps_video_parameter_set_id is u(4).
+HEVC_MAX_VPS_COUNT = 16,
+// 7.4.3.2.1: sps_seq_parameter_set_id is in [0, 15].
+HEVC_MAX_SPS_COUNT = 16,
+// 7.4.3.3.1: pps_pic_parameter_set_id is in [0, 63].
+HEVC_MAX_PPS_COUNT = 64,
+
+// A.4.2: MaxDpbSize is bounded above by 16.
+HEVC_MAX_DPB_SIZE = 16,
+// 7.4.3.1: vps_max_dec_pic_buffering_minus1[i] is in [0, MaxDpbSize - 1].
+HEVC_MAX_REFS = HEVC_MAX_DPB_SIZE,
+
+// 7.4.3.2.1: num_short_term_ref_pic_sets is in [0, 64].
+HEVC_MAX_SHORT_TERM_REF_PIC_SETS = 64,
+// 7.4.3.2.1: num_long_term_ref_pics_sps is in [0, 32].
+HEVC_MAX_LONG_TERM_REF_PICS  = 32,
 
-#define HEVC_MAX_REFS 16
-#define HEVC_MAX_DPB_SIZE 16 // A.4.1
+// A.3: all profiles require that CtbLog2SizeY is in [4, 6].
+HEVC_MIN_LOG2_CTB_SIZE = 4,
+HEVC_MAX_LOG2_CTB_SIZE = 6,
+
+// E.3.2: cpb_cnt_minus1[i] is in [0, 31].
+HEVC_MAX_CPB_CNT = 32,
+
+// A.4.1: table A.6 allows at most 22 tile rows for any level.
+HEVC_MAX_TILE_ROWS= 22,
+// A.4.1: table A.6 allows at most 20 tile columns for any level.
+HEVC_MAX_TILE_COLUMNS = 20,
+
+// 7.4.7.1: in the worst case (tiles_enabled_flag and
+// entropy_coding_sync_enabled_flag are both set), entry points can be
+// placed at the beginning of every Ctb row in every tile, giving an
+// upper bound of (num_tile_columns_minus1 + 1) * PicHeightInCtbsY - 1.
+// Only a stream with very high resolution and perverse parameters could
+// get near that, though, so set a lower limit here with the maximum
+// possible value for 4K video (at most 135 16x16 Ctb rows).
+HEVC_MAX_ENTRY_POINT_OFFSETS = HEVC_MAX_TILE_COLUMNS * 135,
+};
 
-#define HEVC_MAX_LOG2_CTB_SIZE 6
 
 #endif /* AVCODEC_HEVC_H */
diff --git a/libavcodec/hevc_ps.c b/libavcodec/hevc_ps.c
index 3c98e4ce4..74906fd71 100644
--- a/libavcodec/hevc_ps.c
+++ b/libavcodec/hevc_ps.c
@@ -868,7 +868,7 @@ int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, 
unsigned int *sps_id,
 }
 
 sps->nb_st_rps = get_ue_golomb_long(gb);
-if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_RPS_COUNT) {
+if (sps->nb_st_rps > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) {
 av_log(avctx, AV_LOG_ERROR, "Too many short term RPS: %d.\n",
sps->nb_st_rps);
 ret = AVERROR_INVALIDDATA;
diff --git a/libavcodec/hevc_ps.h b/libavcodec/hevc_ps.h
index 89a481ba8..6e2b52777 100644
--- a/libavcodec/hevc_ps.h
+++ b/libavcodec/hevc_ps.h
@@ -166,14 +166,14 @@ typedef struct HEVCSPS {
 ScalingList scaling_list;
 
 unsigned int nb_st_rps;
-ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_RPS_COUNT];
+ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
 
 uint8_t amp_enabled_flag;
 uint8_t sao_enabled;
 
 uint8_t long_term_ref_pics_present_flag;
-uint16_t lt_ref_pic_poc_lsb_sps[32];
-uint8_t used_by_curr_pic_lt_sps_flag[32];
+uint16_t lt_ref_pic_poc_lsb_sps[HEVC_MAX_LONG_TERM_REF_PICS];
+uint8_t used_by_curr_pic_lt_sps_flag[HEVC_MAX_LONG_TERM_REF_PICS];
 uint8_t num_long_term_ref_pics_sps;
 
 struct {
diff --git a/libavformat/hevc.c b/libavformat/hevc.c
index 057f6517f..f8bfeebd3 100644
--- a/libavformat/hevc.c
+++ b/libavformat/hevc.c
@@ -412,7 +412,7 @@ static void skip_scaling_list_data(GetBitContext *gb)
 
 static int parse_rps(GetBitContext *gb, unsigned int rps_idx,
  unsigned int num_rps,
- unsigned int 
num_delta_pocs[HEVC_MAX_SHORT_TERM_RPS_COUNT])
+ unsigned int 
num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
 {
 unsigned int i;
 
@@ -478,7 +478,7 @@ static int hvcc_parse_sps(GetBitContext *gb,
   HEVCDecoderConfiguratio

[libav-devel] [PATCH 01/15] h264: Add stream constraint values to the common header

2017-06-23 Thread Mark Thompson
With comments describing the derivation of each value.
---
 libavcodec/h264.h | 45 +
 1 file changed, 45 insertions(+)

diff --git a/libavcodec/h264.h b/libavcodec/h264.h
index eb3805c06..aa137b5b5 100644
--- a/libavcodec/h264.h
+++ b/libavcodec/h264.h
@@ -44,4 +44,49 @@ enum {
 H264_NAL_AUXILIARY_SLICE = 19,
 };
 
+
+enum {
+// 7.4.2.1.1: seq_parameter_set_id is in [0, 31].
+H264_MAX_SPS_COUNT = 32,
+// 7.4.2.2: pic_parameter_set_id is in [0, 255].
+H264_MAX_PPS_COUNT = 256,
+
+// A.3: MaxDpbFrames is bounded above by 16.
+H264_MAX_DPB_FRAMES = 16,
+// 7.4.2.1.1: max_num_ref_frames is in [0, MaxDpbFrames], and
+// each reference frame can have two fields.
+H264_MAX_REFS   = 2 * H264_MAX_DPB_FRAMES,
+
+// 7.4.3.1: modification_of_pic_nums_idc is not equal to 3 at most
+// num_ref_idx_lN_active_minus1 + 1 times (that is, once for each
+// possible reference), then equal to 3 once.
+H264_MAX_RPLM_COUNT = H264_MAX_REFS + 1,
+
+// 7.4.3.3: in the worst case, we begin with a full short-term
+// reference picture list.  Each picture in turn is moved to the
+// long-term list (type 3) and then discarded from there (type 2).
+// Then, we set the length of the long-term list (type 4), mark
+// the current picture as long-term (type 6) and terminate the
+// process (type 0).
+H264_MAX_MMCO_COUNT = H264_MAX_REFS * 2 + 3,
+
+// A.2.1, A.2.3: profiles supporting FMO constrain
+// num_slice_groups_minus1 to be in [0, 7].
+H264_MAX_SLICE_GROUPS = 8,
+
+// E.2.2: cpb_cnt_minus1 is in [0, 31].
+H264_MAX_CPB_CNT = 32,
+
+// A.3: in table A-1 the highest level allows a MaxFS of 139264.
+H264_MAX_MB_PIC_SIZE = 139264,
+// A.3.1, A.3.2: PicWidthInMbs and PicHeightInMbs are constrained
+// to be not greater than sqrt(MaxFS * 8).  Hence height/width are
+// bounded above by sqrt(139264 * 8) = 1055.5 macroblocks.
+H264_MAX_MB_WIDTH= 1055,
+H264_MAX_MB_HEIGHT   = 1055,
+H264_MAX_WIDTH   = H264_MAX_MB_WIDTH  * 16,
+H264_MAX_HEIGHT  = H264_MAX_MB_HEIGHT * 16,
+};
+
+
 #endif /* AVCODEC_H264_H */
-- 
2.11.0

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

[libav-devel] [PATCH 04/15] lavc: Add coded bitstream read/write API

2017-06-23 Thread Mark Thompson
---
 libavcodec/cbs.c  | 656 ++
 libavcodec/cbs.h  | 274 +++
 libavcodec/cbs_internal.h |  99 +++
 3 files changed, 1029 insertions(+)
 create mode 100644 libavcodec/cbs.c
 create mode 100644 libavcodec/cbs.h
 create mode 100644 libavcodec/cbs_internal.h

diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
new file mode 100644
index 0..ff12bfabf
--- /dev/null
+++ b/libavcodec/cbs.c
@@ -0,0 +1,656 @@
+/*
+ * 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/avassert.h"
+#include "libavutil/common.h"
+
+#include "cbs.h"
+#include "cbs_internal.h"
+#include "golomb.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(&ctx->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(&unit->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, &frag->units[i]);
+av_freep(&frag->units);
+frag->nb_units = 0;
+
+av_freep(&frag->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, &frag->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", i, frag->units[i].type);
+return err;
+}
+}
+
+return 0;
+}
+
+int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
+  CodedBitstreamFragment *frag,
+  const AVCodecParameters *par)
+{
+int err;
+
+memset(frag, 0, sizeof(*frag));
+
+frag->data  = par->extradata;
+frag->data_size = par->extradata_size;
+
+err = ctx->codec->split_fragment(ctx, frag, 1);
+if (err < 0)
+return err;
+
+frag->data  = NULL;
+frag->data_size = 0;
+
+return cbs_read_fragment_content(ctx, frag);
+}
+
+int ff_cbs_read_packet(CodedBitstreamContext *ctx,
+   CodedBitstreamFragment *frag,
+   const AVPacket *pkt)
+{
+int err;
+
+memset(frag, 0, sizeof(*frag));
+
+frag->data  = pkt->data;
+frag->data_size = pkt->size;
+
+err = ctx->codec->split_fragment(ctx, frag, 0);
+if (err < 0)
+return err;
+
+frag->data  =

[libav-devel] [PATCH 12/15] vaapi_h264: Add support for AUD NAL units

2017-06-23 Thread Mark Thompson
Adds a new private option to enable them (off by default).
---
 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 84aa97373..624f2b9a8 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;
 
 #if VA_CHECK_VERSION(0, 36, 0)
@@ -92,6 +94,7 @@ typedef struct VAAPIEncodeH264Options {
 int qp;
 int quality;
 int low_power;
+int aud;
 int sei;
 } VAAPIEncodeH264Options;
 
@@ -148,9 +151,17 @@ static int 
vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
 {
 VAAPIEncodeContext  *ctx = avctx->priv_data;
 VAAPIEncodeH264Context *priv = ctx->priv_data;
+VAAPIEncodeH264Options  *opt = ctx->codec_options;
 CodedBitstreamFragment   *au = &priv->current_access_unit;
 int err;
 
+if (opt->aud && priv->aud_needed) {
+err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
+if (err < 0)
+goto fail;
+priv->aud_needed = 0;
+}
+
 err = vaapi_encode_h264_add_nal(avctx, au, &priv->sps);
 if (err < 0)
 goto fail;
@@ -172,9 +183,17 @@ static int 
vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
 {
 VAAPIEncodeContext  *ctx = avctx->priv_data;
 VAAPIEncodeH264Context *priv = ctx->priv_data;
+VAAPIEncodeH264Options  *opt = ctx->codec_options;
 CodedBitstreamFragment   *au = &priv->current_access_unit;
 int err;
 
+if (opt->aud && priv->aud_needed) {
+err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
+if (err < 0)
+goto fail;
+priv->aud_needed = 0;
+}
+
 err = vaapi_encode_h264_add_nal(avctx, au, &priv->slice);
 if (err < 0)
 goto fail;
@@ -197,6 +216,11 @@ static int 
vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
 int err, i;
 
 if (priv->sei_needed) {
+if (opt->aud && priv->aud_needed) {
+vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
+priv->aud_needed = 0;
+}
+
 memset(&priv->sei, 0, sizeof(priv->sei));
 priv->sei.nal_unit_header.nal_unit_type = H264_NAL_SEI;
 
@@ -577,6 +601,12 @@ 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;
+}
+
 if (opt->sei & SEI_IDENTIFIER && pic->encode_order == 0)
 priv->sei_needed = 1;
 
@@ -936,6 +966,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 14/15] vaapi_h265: Convert to use coded bitstream infrastructure

2017-06-23 Thread Mark Thompson
Also improves the metadata and generally makes the configuration
a bit cleaner.
---
 libavcodec/Makefile|2 +-
 libavcodec/vaapi_encode_h265.c | 1511 +++-
 libavcodec/vaapi_encode_h26x.c |   68 --
 libavcodec/vaapi_encode_h26x.h |   45 --
 4 files changed, 558 insertions(+), 1068 deletions(-)
 delete mode 100644 libavcodec/vaapi_encode_h26x.c
 delete mode 100644 libavcodec/vaapi_encode_h26x.h

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ffca7a103..41331e85c 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -281,7 +281,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..ef8626baa 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 conf_win_right_offset;
-unsigned int conf_win_top_offset;
-unsigned int conf_win_bottom_offset;
-
-// Short-term reference picture sets.
-unsigned int num_short_term_ref_pic_sets;
-VAAPIEncodeH265STRPS st_ref_pic_set[MAX_ST_REF_PIC_SETS];
-
-// Long-term reference pictures.
-char long_term_ref_pics_present_flag;
-unsigned int num_long_term_ref_pics_sps;
-struct {
-unsigned int lt_ref_pic_poc_lsb_sps;
-char used_by_curr_pic_lt_sps_flag;
-} lt_ref_pic;
-
-// Deblocking filter control.
-char deblocking_filter_control_present_flag;
-char deblocking_filter_override_enabled_flag;
-char pps_deblocking_filter_disabled_flag;
-int pps_beta

[libav-devel] [PATCH 00/15] Coded bitstream editing (v4)

2017-06-23 Thread Mark Thompson
Since last time:
* Moved limit values to common headers and documented them.
* Cleaned API, added internal documentation.
* Fixes to syntax files from running on more streams, also a bit more complete 
(yay FMO support).
* Logging fixed to actually work properly (trace_headers uses normal logging 
only now).
* Now used for VAAPI MPEG-2.
* Generally incorporated review comments.

Thanks,

- Mark


Future (no dependency from set as-is):
* H.264 standalone reordering; h264_generate_ts, h264_mark_keyframes BSFs.
* H.265 SEI support, h265_metadata BSF.
* More codecs: JPEG would be nice to complete VAAPI set, VP9 or AV1 might 
actually be useful?
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

[libav-devel] [PATCH 15/15] vaapi_mpeg2: Convert to use coded bitstream infrastructure

2017-06-23 Thread Mark Thompson
---
 libavcodec/vaapi_encode_mpeg2.c | 670 +++-
 1 file changed, 462 insertions(+), 208 deletions(-)

diff --git a/libavcodec/vaapi_encode_mpeg2.c b/libavcodec/vaapi_encode_mpeg2.c
index 634178521..cf1976507 100644
--- a/libavcodec/vaapi_encode_mpeg2.c
+++ b/libavcodec/vaapi_encode_mpeg2.c
@@ -26,6 +26,8 @@
 #include "libavutil/pixfmt.h"
 
 #include "avcodec.h"
+#include "cbs.h"
+#include "cbs_mpeg2.h"
 #include "internal.h"
 #include "mpegvideo.h"
 #include "put_bits.h"
@@ -39,79 +41,104 @@ typedef struct VAAPIEncodeMPEG2Context {
 int quant_p;
 int quant_b;
 
+MPEG2RawSequenceHeader sequence_header;
+MPEG2RawExtensionData  sequence_extension;
+MPEG2RawExtensionData  sequence_display_extension;
+MPEG2RawGroupOfPicturesHeader gop_header;
+MPEG2RawPictureHeader  picture_header;
+MPEG2RawExtensionData  picture_coding_extension;
+
 int64_t last_i_frame;
 
 unsigned int bit_rate;
 unsigned int vbv_buffer_size;
+
+AVRational frame_rate;
+
+unsigned int f_code_horizontal;
+unsigned int f_code_vertical;
+
+CodedBitstreamContext cbc;
+CodedBitstreamFragment current_fragment;
 } VAAPIEncodeMPEG2Context;
 
 
-#define vseq_var(name)  vseq->name, name
-#define vseqext_field(name) vseq->sequence_extension.bits.name, name
-#define vgop_field(name)vseq->gop_header.bits.name, name
-#define vpic_var(name)  vpic->name, name
-#define vpcext_field(name)  vpic->picture_coding_extension.bits.name, name
-#define vcomp_field(name)   vpic->composite_display.bits.name, name
+static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx,
+ char *data, size_t *data_len,
+ CodedBitstreamFragment *frag)
+{
+VAAPIEncodeContext   *ctx = avctx->priv_data;
+VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
+int err;
+
+err = ff_cbs_write_fragment_data(&priv->cbc, frag);
+if (err < 0) {
+av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
+return err;
+}
+
+if (*data_len < 8 * frag->data_size - frag->data_bit_padding) {
+av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
+   "%zu < %zu.\n", *data_len,
+   8 * frag->data_size - frag->data_bit_padding);
+return AVERROR(ENOSPC);
+}
+
+memcpy(data, frag->data, frag->data_size);
+*data_len = 8 * frag->data_size - frag->data_bit_padding;
+
+return 0;
+}
+
+static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx,
+ CodedBitstreamFragment *frag,
+ int type, void *header)
+{
+VAAPIEncodeContext   *ctx = avctx->priv_data;
+VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
+int err;
+
+err = ff_cbs_insert_unit_content(&priv->cbc, frag, -1, type, header);
+if (err < 0) {
+av_log(avctx, AV_LOG_ERROR, "Failed to add header: "
+   "type = %d.\n", type);
+return err;
+}
 
-#define u2(width, value, name) put_bits(&pbc, width, value)
-#define u(width, ...) u2(width, __VA_ARGS__)
+return 0;
+}
 
 static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx,
 char *data, size_t 
*data_len)
 {
-VAAPIEncodeContext *ctx = avctx->priv_data;
-VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params;
-VAAPIEncodeMPEG2Context   *priv = ctx->priv_data;
-PutBitContext pbc;
-
-init_put_bits(&pbc, data, 8 * *data_len);
-
-u(32, SEQ_START_CODE, sequence_header_code);
-
-u(12, vseq->picture_width,  horizontal_size_value);
-u(12, vseq->picture_height, vertical_size_value);
-u(4, vseq_var(aspect_ratio_information));
-u(4, 8, frame_rate_code);
-u(18, priv->bit_rate & 0x3fff, bit_rate_value);
-u(1, 1, marker_bit);
-u(10, priv->vbv_buffer_size & 0x3ff, vbv_buffer_size_value);
-u(1, 0, constrained_parameters_flag);
-u(1, 0, load_intra_quantiser_matrix);
-// intra_quantiser_matrix[64]
-u(1, 0, load_non_intra_quantiser_matrix);
-// non_intra_quantiser_matrix[64]
-
-while (put_bits_count(&pbc) % 8)
-u(1, 0, zero_bit);
-
-u(32, EXT_START_CODE, extension_start_code);
-u(4, 1, extension_start_code_identifier);
-u(8, vseqext_field(profile_and_level_indication));
-u(1, vseqext_field(progressive_sequence));
-u(2, vseqext_field(chroma_format));
-u(2, 0, horizontal_size_extension);
-u(2, 0, vertical_size_extension);
-u(12, priv->bit_rate >> 18, bit_rate_extension);
-u(1, 1, marker_bit);
-u(8, priv->vbv_buffer_size >> 10, vbv_buffer_size_extension);
-u(1, vseqext_field(low_delay));
-u(2, vseqext_field(frame_rate_extension_n));
-u(2, vseqext_field(frame_rate_extension_d));
-
-while (put_bits_count(&pbc) % 8)
-u(1, 0, zero_bit);
-
-u(3

[libav-devel] [PATCH 10/15] lavc: Add h264_redundant_pps bitstream filter

2017-06-23 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.
---
 doc/bitstream_filters.texi  |   9 ++
 libavcodec/Makefile |   2 +
 libavcodec/bitstream_filters.c  |   1 +
 libavcodec/h264_redundant_pps_bsf.c | 176 
 4 files changed, 188 insertions(+)
 create mode 100644 libavcodec/h264_redundant_pps_bsf.c

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 474a9d5c1..623a985a1 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -88,6 +88,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 10a05adf2..31fb412eb 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -771,6 +771,8 @@ OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF)  += 
extract_extradata_bsf.o\
 OBJS-$(CONFIG_H264_METADATA_BSF)  += h264_metadata_bsf.o \
  cbs.o cbs_h2645.o
 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)   += h264_mp4toannexb_bsf.o
+OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF) += h264_redundant_pps_bsf.o \
+ cbs.o cbs_h2645.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 zero and therefore write equivalently.)
+pps->weighted_pred_flag = 1;
+
+return 0;
+}
+
+static int h264_redundant_pps_fixup_slice(H264RedundantPPSContext *ctx,
+  H264RawSliceHeader *slice)
+{
+int qp;
+
+qp = ctx->current_pic_init_qp + slice->slice_qp_delta;
+slice->slice_qp_delta = qp - ctx->global

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

2017-06-23 Thread Mark Thompson
---
 libavcodec/cbs.c |1 +
 libavcodec/cbs_h2645.c   |  410 +++-
 libavcodec/cbs_h265.h|  544 
 libavcodec/cbs_h265_syntax.c | 1482 ++
 libavcodec/cbs_internal.h|1 +
 5 files changed, 2435 insertions(+), 3 deletions(-)
 create mode 100644 libavcodec/cbs_h265.h
 create mode 100644 libavcodec/cbs_h265_syntax.c

diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index 2619b38dd..6030cfd48 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -28,6 +28,7 @@
 
 static const CodedBitstreamType *cbs_type_table[] = {
 &ff_cbs_type_h264,
+&ff_cbs_type_h265,
 };
 
 int ff_cbs_init(CodedBitstreamContext *ctx,
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index ca8d83e9f..190a5a898 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -23,9 +23,11 @@
 #include "cbs.h"
 #include "cbs_internal.h"
 #include "cbs_h264.h"
+#include "cbs_h265.h"
 #include "h264.h"
 #include "h264_sei.h"
 #include "h2645_parse.h"
+#include "hevc.h"
 
 
 #define HEADER(name) do { \
@@ -40,6 +42,7 @@
 
 #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
@@ -102,6 +105,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
@@ -171,6 +178,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
@@ -231,6 +242,40 @@ static void cbs_h264_free_nal_unit(CodedBitstreamUnit 
*unit)
 av_freep(&unit->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(&unit->content);
+}
+
 static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
const H2645Packet *packet)
@@ -345,6 +390,58 @@ static int cbs_h2645_split_fragment(CodedBitstreamContext 
*ctx,
"header.\n", bytestream2_get_bytes_left(&gbc));
 }
 
+} 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(&gbc, frag->data, frag->data_size);
+
+if (bytestream2_get_bytes_left(&gbc) < 23)
+return AVERROR_INVALIDDATA;
+
+version = bytestream2_get_byte(&gbc);
+if (version != 1) {
+av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
+   "first byte %u.", version);
+return AVERROR_INVALIDDATA;
+}
+
+bytestream2_skip(&gbc, 20);
+priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
+
+nb_arrays = bytestream2_get_byte(&gbc);
+for (i = 0; i < nb_arrays; i++) {
+nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
+nb_nals = bytestream2_get_be16(&gbc);
+
+start = bytestream2_tell(&gbc);
+for (j = 0; j < nb_nals; j++) {
+if (bytestream2_get_bytes_left(&gbc) < 2)
+return AVERROR_INVALIDDATA;
+size = bytestream2_get_be16(&gbc);
+if (bytestream2_get_bytes_left(&gbc) < size)
+return AVERROR_INVALIDDATA;
+bytestream2_skip(&gbc, size);
+}
+end = bytestream2_tell(&gbc);
+
+err = ff_h2645_packet_split(&priv->read_packet,
+frag->data + start, end - start,
+ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC);
+if (err 

[libav-devel] [PATCH 09/15] lavc: Add h264_metadata bitstream filter

2017-06-23 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.
---
 doc/bitstream_filters.texi |  47 +
 libavcodec/Makefile|   2 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/h264_metadata_bsf.c | 462 +
 4 files changed, 512 insertions(+)
 create mode 100644 libavcodec/h264_metadata_bsf.c

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index af14b67ad..474a9d5c1 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -39,6 +39,53 @@ When this option is enabled, the long-term headers are 
removed from the
 bitstream after extraction.
 @end table
 
+@section h264_metadata
+
+Modify metadata attached to the 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 in 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 frame_rate
+@item fixed_frame_rate_flag
+Set the frame rate in the VUI parameters (num_units_in_tick /
+time_scale).  Note that this is likely to be overridden by container
+parameters when the stream is in a container.
+
+@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 b6c3d..10a05adf2 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -768,6 +768,8 @@ 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 \
+ cbs.o cbs_h2645.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..4eb58ce45
--- /dev/null
+++ b/libavcodec/h264_metadata_bsf.c
@@ -0,0 +1,462 @@
+/*
+ * 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 {
+const AVClass *class;
+
+CodedBitstreamContext cbc;
+CodedBitstreamFragment access_unit;
+
+H264RawAUD aud_nal;
+H264RawSEI sei_nal;
+
+int aud;
+
+AVRational sample_aspect_ratio;
+
+int video_format;
+ 

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

2017-06-23 Thread Mark Thompson
---
 libavcodec/cbs.c  |   1 +
 libavcodec/cbs_internal.h |   1 +
 libavcodec/cbs_mpeg2.c| 355 
 libavcodec/cbs_mpeg2.h| 207 
 libavcodec/cbs_mpeg2_syntax.c | 367 ++
 5 files changed, 931 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[] = {
 &ff_cbs_type_h264,
 &ff_cbs_type_h265,
+&ff_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, \
+   &value, 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", &one, 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 = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
+   &start_code);
+for (i = 0;; i++) {
+end = avpriv_find_start_code(start, frag->data + frag->data_size,
+ &next_start_code);
+
+unit_type = start_code & 0xff;
+
+if (end == frag->data + frag->data_size)

[libav-devel] [PATCH 13/15] vaapi_h264: Add support for SEI recovery points

2017-06-23 Thread Mark Thompson
Included by default with non-IDR intra frames.
---
 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 624f2b9a8..7c9c96a3a 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;
 
@@ -240,6 +242,11 @@ static int 
vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
 priv->sei.payload[i].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].recovery_point = priv->recovery_point;
+++i;
+}
 
 priv->sei.payload_count = i;
 av_assert0(priv->sei.payload_count > 0);
@@ -621,6 +628,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,
@@ -971,7 +986,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 },
@@ -979,6 +994,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 08/15] lavc: Add trace_headers bitstream filter

2017-06-23 Thread Mark Thompson
Supports all streams that the coded bitstream infrastructure does
(currently H.264, H.265 and MPEG-2).
---
 doc/bitstream_filters.texi |   8 +++
 libavcodec/Makefile|   2 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/trace_headers_bsf.c | 113 +
 4 files changed, 124 insertions(+)
 create mode 100644 libavcodec/trace_headers_bsf.c

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 64f91f4b5..af14b67ad 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, H.265 and MPEG-2.
+
 @section vp9_superframe
 
 Combine VP9 frames into superframes.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 240f7d83a..b6c3d 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -778,6 +778,8 @@ 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 \
+ cbs.o cbs_h2645.o cbs_mpeg2.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..6d418c596
--- /dev/null
+++ b/libavcodec/trace_headers_bsf.c
@@ -0,0 +1,113 @@
+/*
+ * 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/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(&ctx->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(&ctx->cbc, &ps, bsf->par_in);
+if (err < 0) {
+av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
+return err;
+}
+
+ff_cbs_fragment_uninit(&ctx->cbc, &ps);
+}
+
+return 0;
+}
+
+static void trace_headers_close(AVBSFContext *bsf)
+{
+TraceHeadersContext *ctx = bsf->priv_data;
+
+ff_cbs_close(&ctx->cbc);
+}
+
+static int trace_headers(AVBSFContext *bsf, AVPacket *out)
+{
+TraceHeadersContext *ctx = bsf->priv_data;
+CodedBitstreamFragment au;
+AVPacket *in;
+int err;
+
+err = ff_bsf_get_packet(bsf, &in);
+if (err < 0)
+return err;
+
+if (in->pts == AV_NOPTS_VALUE)
+av_log(bsf, AV_LOG_INFO, "Packet (%sno pts)\n",
+   in->flags & AV_PKT_FLAG_KEY ? "key frame, " : "");
+else
+av_log(bsf, AV_LOG_INFO, "Packet (%spts %"PRId64")\n",
+   in->flags & AV_PKT_FLAG_KEY ? "key frame, " : "", in->pts);
+
+err = ff_cbs_rea

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

2017-06-23 Thread Mark Thompson
---
 libavcodec/cbs.c |1 +
 libavcodec/cbs_h264.h|  426 +++
 libavcodec/cbs_h2645.c   |  800 +++
 libavcodec/cbs_h2645.h   |   42 ++
 libavcodec/cbs_h264_syntax.c | 1224 ++
 libavcodec/cbs_internal.h|3 +
 6 files changed, 2496 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/libavcodec/cbs.c b/libavcodec/cbs.c
index ff12bfabf..2619b38dd 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -27,6 +27,7 @@
 
 
 static const CodedBitstreamType *cbs_type_table[] = {
+&ff_cbs_type_h264,
 };
 
 int ff_cbs_init(CodedBitstreamContext *ctx,
diff --git a/libavcodec/cbs_h264.h b/libavcodec/cbs_h264.h
new file mode 100644
index 0..bcceacc83
--- /dev/null
+++ b/libavcodec/cbs_h264.h
@@ -0,0 +1,426 @@
+/*
+ * 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 "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;
+uint32_t num_units_in_tick;
+uint32_t time_scale;
+uint8_t fixed_frame_rate_flag;
+
+uint8_t nal_hrd_parameters_present_flag;
+H264RawHRD nal_hrd_parameters;
+uint8_t vcl_hrd_parameters_present_flag;
+H264RawHRD vcl_hrd_parameters;
+uint8_t low_delay_hrd_flag;
+
+uint8_t pic_struct_present_flag;
+
+uint8_t bitstream_restriction_flag;
+uint8_t motion_vectors_over_pic_boundaries_flag;
+uint8_t max_bytes_per_pic_denom;
+uint8_t max_bits_per_mb_denom;
+uint8_t log2_max_mv_length_horizontal;
+uint8_t log2_max_mv_length_vertical;
+uint8_t max_num_reorder_frames;
+uint8_t max_dec_frame_buffering;
+} H264RawVUI;
+
+typedef struct H264RawSPS {
+H264RawNALUnitHeader nal_unit_header;
+
+uint8_t profile_idc;
+uint8_t constraint_set0_flag;
+uint8_t constraint_set1_flag;
+uint8_t constraint_set2_flag;
+uint8_t constraint_set3_flag;
+uint8_t constraint_set4_flag;
+uint8_t constraint_set5_flag;
+uint8_t reserved_zero_2bits;
+uint8_t level_idc;
+
+uint8_t seq_parameter_set_id;
+
+uint8_t chroma_format_idc;
+uint8_t separate_colour_plane_flag;
+uint8_t bit_depth_luma_minus8;
+uint8_t bit_depth_chroma_minus8;
+uint8_t qpprime_y_zero_t

[libav-devel] [PATCH 11/15] vaapi_h264: Convert to use coded bitstream infrastructure

2017-06-23 Thread Mark Thompson
---
 libavcodec/Makefile|2 +-
 libavcodec/vaapi_encode_h264.c | 1419 
 2 files changed, 547 insertions(+), 874 deletions(-)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 31fb412eb..ffca7a103 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -271,7 +271,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 7583a20c1..84aa97373 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;
-unsigned int nal_ref_idc;
-
-unsigned int colour_plane_id;
-char field_pic_flag;
-char bottom_field_flag;
-
-unsigned int redundant_pic_cnt;
-
-char sp_for_switch_flag;
-int slice_qs_delta;
-
-char ref_pic_list_modification_flag_l0;
-char ref_pic_list_modification_flag_l1;
-
-char no_output_of_prior_pics_flag;
-char long_term_reference_flag;
-char adaptive_ref_pic_marking_mode_flag;
-} VAAPIEncodeH264MiscSliceParams;
-
-typedef struct VAAPIEncodeH264Slice {
-VAAPIEncodeH264MiscSliceParams misc_slice_params;
-} VAAPIEncodeH264Slice;
+// Random (version 4) ISO 11578 UUID.
+static const uint8_t vaapi_encode_

[libav-devel] [PATCH 03/15] hevc: Validate the number of long term reference pictures

2017-06-23 Thread Mark Thompson
This would overflow if the stream contained a value greater than the
maximum allowed by the standard (32).
---
 libavcodec/hevc_ps.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/libavcodec/hevc_ps.c b/libavcodec/hevc_ps.c
index 74906fd71..2603e6d99 100644
--- a/libavcodec/hevc_ps.c
+++ b/libavcodec/hevc_ps.c
@@ -883,6 +883,12 @@ int ff_hevc_parse_sps(HEVCSPS *sps, GetBitContext *gb, 
unsigned int *sps_id,
 sps->long_term_ref_pics_present_flag = get_bits1(gb);
 if (sps->long_term_ref_pics_present_flag) {
 sps->num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
+if (sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PICS) {
+av_log(avctx, AV_LOG_ERROR, "Too many long term ref pics: %d.\n",
+   sps->num_long_term_ref_pics_sps);
+ret = AVERROR_INVALIDDATA;
+goto err;
+}
 for (i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
 sps->lt_ref_pic_poc_lsb_sps[i]   = get_bits(gb, 
sps->log2_max_poc_lsb);
 sps->used_by_curr_pic_lt_sps_flag[i] = get_bits1(gb);
-- 
2.11.0

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

Re: [libav-devel] [PATCH] NVENC Input Surface Optimization

2017-06-23 Thread Ben Chang
Thanks Lu. I will go through the submitted changes to understand what the 
styling issues are. 

-Original Message-
From: libav-devel [mailto:libav-devel-boun...@libav.org] On Behalf Of Luca 
Barbato
Sent: Thursday, June 22, 2017 5:39 PM
To: libav-devel@libav.org
Subject: Re: [libav-devel] [PATCH] NVENC Input Surface Optimization

On 6/23/17 1:47 AM, Ben Chang wrote:
> Hi,
> 
> Please help review this nvenc patch for input surface optimization.
> 
> This patch aims to reduce the number of input/output surfaces NVENC 
> allocates per session. Previous default sets allocated surfaces to 32 
> (unless there is user specified param or lookahead involved). Having 
> large number of surfaces consumes extra video memory (esp for higher 
> resolution encoding). The patch changes the surfaces calculation for 
> default, B-frames, lookahead scenario respectively.
> 
> Change in location of surfaces and async_depth determination. Putting 
> it prior to rate control setup. Current code limits lookahead depth to 
> be 27 (default surface (32) - P interval (1) - 4).
> 
> The other change involves surface selection. Previously, if a session 
> allocates x surfaces, only x-1 surfaces are used (due to combination 
> of output delay and lock toggle logic). To prevent unused surfaces, 
> changing surface rotation to using predefined fifo.
> 
> Miscellaneous -  add "auto" option in level. (was approved in a 
> previous patch that never propagated into the repository)

There are some style issue, beside that the logic seems fine.

I'll amend and split the patch in a set of 3, please ping me if it doesn't land 
during the weekend.

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

---
This email message is for the sole use of the intended recipient(s) and may 
contain
confidential information.  Any unauthorized review, use, disclosure or 
distribution
is prohibited.  If you are not the intended recipient, please contact the 
sender by
reply email and destroy all copies of the original message.
---
___
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

[libav-devel] [PATCH] configure: fix assignment of assembler specific flags

2017-06-23 Thread James Almer
If the first assembler to be probed is an old nasm build, X86ASM_DEPFLAGS
will be set and remain so after yasm is ultimately used as fallback.
This results in yasm being called with said nasm specific flags and failing
during actual object assembly but not with configure sanity checks.

Regression since adfd7892e3b8b40e7a1620f7254459d8e096a9a1

Signed-off-by: James Almer 
---
 configure | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index ce0f6c919..3a6df98a9 100755
--- a/configure
+++ b/configure
@@ -4515,12 +4515,10 @@ EOF
 x86asmexe=$x86asmexe_probe
 x86asm_type=nasm
 x86asm_debug="-g -F dwarf"
-X86ASM_DEPFLAGS='-MD $(@:.o=.d)'
 elif check_cmd $x86asmexe_probe --version; then
 x86asmexe=$x86asmexe_probe
 x86asm_type=yasm
 x86asm_debug="-g dwarf2"
-X86ASMDEP='$(DEPX86ASM) $(X86ASMFLAGS) -M $(X86ASM_O) $< > 
$(@:.o=.d)'
 fi
 check_x86asm "movbe ecx, [5]" && enable x86asm
 }
@@ -4531,6 +4529,8 @@ EOF
 probe_x86asm $program && break
 done
 disabled x86asm && die "nasm/yasm not found or too old. Use 
--disable-x86asm for a crippled build."
+test $x86asm_type = 'nasm' && X86ASM_DEPFLAGS='-MD $(@:.o=.d)'
+test $x86asm_type = 'yasm' && X86ASMDEP='$(DEPX86ASM) $(X86ASMFLAGS) 
-M $(X86ASM_O) $< > $(@:.o=.d)'
 X86ASMFLAGS="-f $objformat"
 enabled pic   && append X86ASMFLAGS "-DPIC"
 test -n "$extern_prefix"  && append X86ASMFLAGS "-DPREFIX"
-- 
2.13.0

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

Re: [libav-devel] [v12] [PATCH 1/2] build: Add an option for passing linker flags to the shared library build

2017-06-23 Thread Diego Biurrun
On Fri, Jun 23, 2017 at 02:06:28PM +0200, Diego Biurrun wrote:
> From: Janne Grunau 
> 
> Also employ this mechanism to pass $libdir to the runtime library search
> path if rpath is enabled. This fixes underlinking of some test binaries
> on some systems.
> 
> (cherry picked from commit 857e26b655a769e5a56bada1a0d9adb44cc176b7)
> Signed-off-by: Diego Biurrun 
> ---
>  configure   | 11 ++-
>  library.mak |  2 +-
>  2 files changed, 11 insertions(+), 2 deletions(-)

This also needs porting to v11.

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

[libav-devel] [v12] [PATCH 1/2] build: Add an option for passing linker flags to the shared library build

2017-06-23 Thread Diego Biurrun
From: Janne Grunau 

Also employ this mechanism to pass $libdir to the runtime library search
path if rpath is enabled. This fixes underlinking of some test binaries
on some systems.

(cherry picked from commit 857e26b655a769e5a56bada1a0d9adb44cc176b7)
Signed-off-by: Diego Biurrun 
---
 configure   | 11 ++-
 library.mak |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index 660b062..8a1c884 100755
--- a/configure
+++ b/configure
@@ -277,6 +277,7 @@ Toolchain options:
   --extra-objcflags=FLAGS  add FLAGS to OBJCFLAGS [$CFLAGS]
   --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS [$LDFLAGS]
   --extra-ldexeflags=ELDFLAGS add ELDFLAGS to LDEXEFLAGS [$LDEXEFLAGS]
+  --extra-ldsoflags=ELDFLAGS add ELDFLAGS to LDSOFLAGS [$LDSOFLAGS]
   --extra-libs=ELIBS   add ELIBS [$ELIBS]
   --extra-version=STRING   version string suffix []
   --optflags=OPTFLAGS  override optimization-related compiler flags
@@ -730,6 +731,10 @@ add_ldexeflags(){
 append LDEXEFLAGS $($ldflags_filter "$@")
 }
 
+add_ldsoflags(){
+append LDSOFLAGS $($ldflags_filter "$@")
+}
+
 add_stripflags(){
 append STRIPFLAGS "$@"
 }
@@ -2681,6 +2686,9 @@ for opt do
 --extra-ldexeflags=*)
 add_ldexeflags $optval
 ;;
+--extra-ldsoflags=*)
+add_ldsoflags $optval
+;;
 --extra-libs=*)
 add_extralibs $optval
 ;;
@@ -4829,7 +4837,7 @@ check_disable_warning -Wno-pointer-sign
 # add some linker flags
 check_ldflags -Wl,--warn-common
 check_ldflags 
-Wl,-rpath-link=libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil:libavresample
-enabled rpath && add_ldexeflags -Wl,-rpath,$libdir
+enabled rpath && add_ldexeflags -Wl,-rpath,$libdir && add_ldsoflags 
-Wl,-rpath,$libdir
 test_ldflags -Wl,-Bsymbolic && append SHFLAGS -Wl,-Bsymbolic
 
 # add some strip flags
@@ -5225,6 +5233,7 @@ LD_PATH=$LD_PATH
 DLLTOOL=$dlltool
 LDFLAGS=$LDFLAGS
 LDEXEFLAGS=$LDEXEFLAGS
+LDSOFLAGS=$LDSOFLAGS
 SHFLAGS=$(echo $($ldflags_filter $SHFLAGS))
 STRIPFLAGS=$STRIPFLAGS
 YASMFLAGS=$YASMFLAGS
diff --git a/library.mak b/library.mak
index 44087aa..07e34bd 100644
--- a/library.mak
+++ b/library.mak
@@ -43,7 +43,7 @@ $(SUBDIR)$(SLIBNAME): $(SUBDIR)$(SLIBNAME_WITH_MAJOR)
 
 $(SUBDIR)$(SLIBNAME_WITH_MAJOR): $(OBJS) $(SUBDIR)lib$(NAME).ver $(DEP_LIBS)
$(SLIB_CREATE_DEF_CMD)
-   $$(LD) $(SHFLAGS) $(LDFLAGS) $$(LD_O) $$(filter %.o,$$^) $(FFEXTRALIBS)
+   $$(LD) $(SHFLAGS) $(LDFLAGS) $(LDSOFLAGS) $$(LD_O) $$(filter %.o,$$^) 
$(FFEXTRALIBS)
$(SLIB_EXTRA_CMD)
 
 clean::
-- 
2.1.4

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

[libav-devel] [v12] [PATCH 2/2] configure: Simplify and fix libxcb check

2017-06-23 Thread Diego Biurrun
Check for xcb as well as xcb-shape before enabling libxcb since newer
versions of libxcb have xcb-foo pkg-config files that do not declare
their xcb dependency so that required linker flags will not be generated.

Use helper functions to simplify libxcb check, drop unused variables.

(cherry picked from commit 1ea77aae927c7310034b1f75d4f1c2676fe641f2)
(cherry picked from commit a97563c889fefd81ad6b3758471434d8c2e2e550)
(cherry picked from commit 871b4f3654636ed64560e86b9faa33828d195ceb)
Signed-off-by: Diego Biurrun 
---
 configure | 9 -
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/configure b/configure
index 8a1c884..31d4229 100755
--- a/configure
+++ b/configure
@@ -4769,9 +4769,8 @@ fi
 check_lib X11/Xlib.h XOpenDisplay -lX11 && enable xlib
 
 if enabled libxcb; then
-check_pkg_config xcb-shape xcb/shape.h xcb_shape_rectangles || {
-enabled libxcb && die "ERROR: libxcb not found";
-} && enable libxcb
+require_pkg_config xcb xcb/xcb.h xcb_connect
+require_pkg_config xcb-shape xcb/shape.h xcb_shape_rectangles
 
 disabled libxcb_shm ||
 check_pkg_config xcb-shm xcb/shm.h xcb_shm_attach || {
@@ -4783,8 +4782,8 @@ if enabled libxcb; then
 enabled libxcb_xfixes && die "ERROR: libxcb_xfixes not found";
 } && enable libxcb_xfixes
 
-add_cflags "$xcb_event_cflags $xcb_shm_cflags $xcb_xfixes_cflags"
-add_extralibs "$xcb_event_libs $xcb_shm_libs $xcb_xfixes_libs"
+add_cflags "$xcb_shm_cflags $xcb_xfixes_cflags"
+add_extralibs "$xcb_shm_libs $xcb_xfixes_libs"
 fi
 
 enabled vaapi &&
-- 
2.1.4

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