1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-21 10:55:51 +02:00

lavc: Add coded bitstream read/write support for H.264

This commit is contained in:
Mark Thompson 2017-05-14 16:18:25 +01:00
parent 18f1706f33
commit acf06f4544
8 changed files with 2706 additions and 0 deletions

2
configure vendored
View File

@ -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"

View File

@ -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

View File

@ -28,6 +28,9 @@
static const CodedBitstreamType *cbs_type_table[] = {
#if CONFIG_CBS_H264
&ff_cbs_type_h264,
#endif
};
int ff_cbs_init(CodedBitstreamContext *ctx,

427
libavcodec/cbs_h264.h Normal file
View File

@ -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 <stddef.h>
#include <stdint.h>
#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_transform_bypass_flag;
uint8_t seq_scaling_matrix_present_flag;
uint8_t seq_scaling_list_present_flag[12];
H264RawScalingList scaling_list_4x4[6];
H264RawScalingList scaling_list_8x8[6];
uint8_t log2_max_frame_num_minus4;
uint8_t pic_order_cnt_type;
uint8_t log2_max_pic_order_cnt_lsb_minus4;
uint8_t delta_pic_order_always_zero_flag;
int32_t offset_for_non_ref_pic;
int32_t offset_for_top_to_bottom_field;
uint8_t num_ref_frames_in_pic_order_cnt_cycle;
int32_t offset_for_ref_frame[256];
uint8_t max_num_ref_frames;
uint8_t gaps_in_frame_num_allowed_flag;
uint16_t pic_width_in_mbs_minus1;
uint16_t pic_height_in_map_units_minus1;
uint8_t frame_mbs_only_flag;
uint8_t mb_adaptive_frame_field_flag;
uint8_t direct_8x8_inference_flag;
uint8_t frame_cropping_flag;
uint16_t frame_crop_left_offset;
uint16_t frame_crop_right_offset;
uint16_t frame_crop_top_offset;
uint16_t frame_crop_bottom_offset;
uint8_t vui_parameters_present_flag;
H264RawVUI vui;
} H264RawSPS;
typedef struct H264RawSPSExtension {
H264RawNALUnitHeader nal_unit_header;
uint8_t seq_parameter_set_id;
uint8_t aux_format_idc;
uint8_t bit_depth_aux_minus8;
uint8_t alpha_incr_flag;
uint16_t alpha_opaque_value;
uint16_t alpha_transparent_value;
uint8_t additional_extension_flag;
} H264RawSPSExtension;
typedef struct H264RawPPS {
H264RawNALUnitHeader nal_unit_header;
uint8_t pic_parameter_set_id;
uint8_t seq_parameter_set_id;
uint8_t entropy_coding_mode_flag;
uint8_t bottom_field_pic_order_in_frame_present_flag;
uint8_t num_slice_groups_minus1;
uint8_t slice_group_map_type;
uint16_t run_length_minus1[H264_MAX_SLICE_GROUPS];
uint16_t top_left[H264_MAX_SLICE_GROUPS];
uint16_t bottom_right[H264_MAX_SLICE_GROUPS];
uint8_t slice_group_change_direction_flag;
uint16_t slice_group_change_rate_minus1;
uint16_t pic_size_in_map_units_minus1;
uint8_t slice_group_id[H264_MAX_MB_PIC_SIZE];
uint8_t num_ref_idx_l0_default_active_minus1;
uint8_t num_ref_idx_l1_default_active_minus1;
uint8_t weighted_pred_flag;
uint8_t weighted_bipred_idc;
int8_t pic_init_qp_minus26;
int8_t pic_init_qs_minus26;
int8_t chroma_qp_index_offset;
uint8_t deblocking_filter_control_present_flag;
uint8_t constrained_intra_pred_flag;
uint8_t more_rbsp_data;
uint8_t redundant_pic_cnt_present_flag;
uint8_t transform_8x8_mode_flag;
uint8_t pic_scaling_matrix_present_flag;
uint8_t pic_scaling_list_present_flag[12];
H264RawScalingList scaling_list_4x4[6];
H264RawScalingList scaling_list_8x8[6];
int8_t second_chroma_qp_index_offset;
} H264RawPPS;
typedef struct H264RawAUD {
H264RawNALUnitHeader nal_unit_header;
uint8_t primary_pic_type;
} H264RawAUD;
typedef struct H264RawSEIBufferingPeriod {
uint8_t seq_parameter_set_id;
struct {
uint32_t initial_cpb_removal_delay[H264_MAX_CPB_CNT];
uint32_t initial_cpb_removal_delay_offset[H264_MAX_CPB_CNT];
} nal, vcl;
} H264RawSEIBufferingPeriod;
typedef struct H264RawSEIPicTimestamp {
uint8_t ct_type;
uint8_t nuit_field_based_flag;
uint8_t counting_type;
uint8_t full_timestamp_flag;
uint8_t discontinuity_flag;
uint8_t cnt_dropped_flag;
uint8_t n_frames;
uint8_t seconds_flag;
uint8_t seconds_value;
uint8_t minutes_flag;
uint8_t minutes_value;
uint8_t hours_flag;
uint8_t hours_value;
uint32_t time_offset;
} H264RawSEIPicTimestamp;
typedef struct H264RawSEIPicTiming {
uint32_t cpb_removal_delay;
uint32_t dpb_output_delay;
uint8_t pic_struct;
uint8_t clock_timestamp_flag[3];
H264RawSEIPicTimestamp timestamp[3];
} H264RawSEIPicTiming;
typedef struct H264RawSEIUserDataRegistered {
uint8_t itu_t_t35_country_code;
uint8_t itu_t_t35_country_code_extension_byte;
uint8_t *data;
size_t data_length;
} H264RawSEIUserDataRegistered;
typedef struct H264RawSEIUserDataUnregistered {
uint8_t uuid_iso_iec_11578[16];
uint8_t *data;
size_t data_length;
} H264RawSEIUserDataUnregistered;
typedef struct H264RawSEIRecoveryPoint {
uint16_t recovery_frame_cnt;
uint8_t exact_match_flag;
uint8_t broken_link_flag;
uint8_t changing_slice_group_idc;
} H264RawSEIRecoveryPoint;
typedef struct H264RawSEIDisplayOrientation {
uint8_t display_orientation_cancel_flag;
uint8_t hor_flip;
uint8_t ver_flip;
uint16_t anticlockwise_rotation;
uint16_t display_orientation_repetition_period;
uint8_t display_orientation_extension_flag;
} H264RawSEIDisplayOrientation;
typedef struct H264RawSEIPayload {
uint32_t payload_type;
uint32_t payload_size;
union {
H264RawSEIBufferingPeriod buffering_period;
H264RawSEIPicTiming pic_timing;
// H264RawSEIFiller filler -> no fields.
H264RawSEIUserDataRegistered user_data_registered;
H264RawSEIUserDataUnregistered user_data_unregistered;
H264RawSEIRecoveryPoint recovery_point;
H264RawSEIDisplayOrientation display_orientation;
struct {
uint8_t *data;
size_t data_length;
} other;
} payload;
} H264RawSEIPayload;
typedef struct H264RawSEI {
H264RawNALUnitHeader nal_unit_header;
H264RawSEIPayload payload[H264_MAX_SEI_PAYLOADS];
uint8_t payload_count;
} H264RawSEI;
typedef struct H264RawSliceHeader {
H264RawNALUnitHeader nal_unit_header;
uint32_t first_mb_in_slice;
uint8_t slice_type;
uint8_t pic_parameter_set_id;
uint8_t colour_plane_id;
uint16_t frame_num;
uint8_t field_pic_flag;
uint8_t bottom_field_flag;
uint16_t idr_pic_id;
uint16_t pic_order_cnt_lsb;
int32_t delta_pic_order_cnt_bottom;
int32_t delta_pic_order_cnt[2];
uint8_t redundant_pic_cnt;
uint8_t direct_spatial_mv_pred_flag;
uint8_t num_ref_idx_active_override_flag;
uint8_t num_ref_idx_l0_active_minus1;
uint8_t num_ref_idx_l1_active_minus1;
uint8_t ref_pic_list_modification_flag_l0;
uint8_t ref_pic_list_modification_flag_l1;
struct {
uint8_t modification_of_pic_nums_idc;
int32_t abs_diff_pic_num_minus1;
uint8_t long_term_pic_num;
} rplm_l0[H264_MAX_RPLM_COUNT], rplm_l1[H264_MAX_RPLM_COUNT];
uint8_t luma_log2_weight_denom;
uint8_t chroma_log2_weight_denom;
uint8_t luma_weight_l0_flag[H264_MAX_REFS];
int8_t luma_weight_l0[H264_MAX_REFS];
int8_t luma_offset_l0[H264_MAX_REFS];
uint8_t chroma_weight_l0_flag[H264_MAX_REFS];
int8_t chroma_weight_l0[H264_MAX_REFS][2];
int8_t chroma_offset_l0[H264_MAX_REFS][2];
uint8_t luma_weight_l1_flag[H264_MAX_REFS];
int8_t luma_weight_l1[H264_MAX_REFS];
int8_t luma_offset_l1[H264_MAX_REFS];
uint8_t chroma_weight_l1_flag[H264_MAX_REFS];
int8_t chroma_weight_l1[H264_MAX_REFS][2];
int8_t chroma_offset_l1[H264_MAX_REFS][2];
uint8_t no_output_of_prior_pics_flag;
uint8_t long_term_reference_flag;
uint8_t adaptive_ref_pic_marking_mode_flag;
struct {
uint8_t memory_management_control_operation;
int32_t difference_of_pic_nums_minus1;
uint8_t long_term_pic_num;
uint8_t long_term_frame_idx;
uint8_t max_long_term_frame_idx_plus1;
} mmco[H264_MAX_MMCO_COUNT];
uint8_t cabac_init_idc;
int8_t slice_qp_delta;
uint8_t sp_for_switch_flag;
int8_t slice_qs_delta;
uint8_t disable_deblocking_filter_idc;
int8_t slice_alpha_c0_offset_div2;
int8_t slice_beta_offset_div2;
uint16_t slice_group_change_cycle;
} H264RawSliceHeader;
typedef struct H264RawSlice {
H264RawSliceHeader header;
uint8_t *data;
size_t data_size;
int data_bit_start;
} H264RawSlice;
typedef struct CodedBitstreamH264Context {
// Reader/writer context in common with the H.265 implementation.
CodedBitstreamH2645Context common;
// All currently available parameter sets. These are updated when
// any parameter set NAL unit is read/written with this context.
H264RawSPS *sps[H264_MAX_SPS_COUNT];
H264RawPPS *pps[H264_MAX_PPS_COUNT];
// The currently active parameter sets. These are updated when any
// NAL unit refers to the relevant parameter set. These pointers
// must also be present in the arrays above.
const H264RawSPS *active_sps;
const H264RawPPS *active_pps;
// The NAL unit type of the most recent normal slice. This is required
// to be able to read/write auxiliary slices, because IdrPicFlag is
// otherwise unknown.
uint8_t last_slice_nal_unit_type;
} CodedBitstreamH264Context;
#endif /* AVCODEC_CBS_H264_H */

997
libavcodec/cbs_h2645.c Normal file
View File

@ -0,0 +1,997 @@
/*
* 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/attributes.h"
#include "libavutil/avassert.h"
#include "bytestream.h"
#include "cbs.h"
#include "cbs_internal.h"
#include "cbs_h264.h"
#include "golomb.h"
#include "h264.h"
#include "h264_sei.h"
#include "h2645_parse.h"
static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, BitstreamContext *bc,
const char *name, uint32_t *write_to,
uint32_t range_min, uint32_t range_max)
{
uint32_t value;
int position;
if (ctx->trace_enable) {
char bits[65];
unsigned int k;
int i, j;
position = bitstream_tell(bc);
for (i = 0; i < 32; i++) {
k = bitstream_read_bit(bc);
bits[i] = k ? '1' : '0';
if (k)
break;
}
if (i >= 32) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb "
"code found while reading %s: "
"more than 31 zeroes.\n", name);
return AVERROR_INVALIDDATA;
}
value = 1;
for (j = 0; j < i; j++) {
k = bitstream_read_bit(bc);
bits[i + j + 1] = k ? '1' : '0';
value = value << 1 | k;
}
bits[i + j + 1] = 0;
--value;
ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
} else {
value = get_ue_golomb_long(bc);
}
if (value < range_min || value > range_max) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
"%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
name, value, range_min, range_max);
return AVERROR_INVALIDDATA;
}
*write_to = value;
return 0;
}
static int cbs_read_se_golomb(CodedBitstreamContext *ctx, BitstreamContext *bc,
const char *name, int32_t *write_to,
int32_t range_min, int32_t range_max)
{
int32_t value;
int position;
if (ctx->trace_enable) {
char bits[65];
uint32_t v;
unsigned int k;
int i, j;
position = bitstream_tell(bc);
for (i = 0; i < 32; i++) {
k = bitstream_read_bit(bc);
bits[i] = k ? '1' : '0';
if (k)
break;
}
if (i >= 32) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb "
"code found while reading %s: "
"more than 31 zeroes.\n", name);
return AVERROR_INVALIDDATA;
}
v = 1;
for (j = 0; j < i; j++) {
k = bitstream_read_bit(bc);
bits[i + j + 1] = k ? '1' : '0';
v = v << 1 | k;
}
bits[i + j + 1] = 0;
if (v & 1)
value = -(int32_t)(v / 2);
else
value = v / 2;
ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
} else {
value = get_se_golomb_long(bc);
}
if (value < range_min || value > range_max) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
"%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
name, value, range_min, range_max);
return AVERROR_INVALIDDATA;
}
*write_to = value;
return 0;
}
static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
const char *name, uint32_t value,
uint32_t range_min, uint32_t range_max)
{
int len;
if (value < range_min || value > range_max) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
"%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
name, value, range_min, range_max);
return AVERROR_INVALIDDATA;
}
av_assert0(value != UINT32_MAX);
len = av_log2(value + 1);
if (put_bits_left(pbc) < 2 * len + 1)
return AVERROR(ENOSPC);
if (ctx->trace_enable) {
char bits[65];
int i;
for (i = 0; i < len; i++)
bits[i] = '0';
bits[len] = '1';
for (i = 0; i < len; i++)
bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
bits[len + len + 1] = 0;
ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
}
put_bits(pbc, len, 0);
if (len + 1 < 32)
put_bits(pbc, len + 1, value + 1);
else
put_bits32(pbc, value + 1);
return 0;
}
static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
const char *name, int32_t value,
int32_t range_min, int32_t range_max)
{
int len;
uint32_t uvalue;
if (value < range_min || value > range_max) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
"%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
name, value, range_min, range_max);
return AVERROR_INVALIDDATA;
}
av_assert0(value != INT32_MIN);
if (value == 0)
uvalue = 0;
else if (value > 0)
uvalue = 2 * (uint32_t)value - 1;
else
uvalue = 2 * (uint32_t)-value;
len = av_log2(uvalue + 1);
if (put_bits_left(pbc) < 2 * len + 1)
return AVERROR(ENOSPC);
if (ctx->trace_enable) {
char bits[65];
int i;
for (i = 0; i < len; i++)
bits[i] = '0';
bits[len] = '1';
for (i = 0; i < len; i++)
bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
bits[len + len + 1] = 0;
ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
}
put_bits(pbc, len, 0);
if (len + 1 < 32)
put_bits(pbc, len + 1, uvalue + 1);
else
put_bits32(pbc, uvalue + 1);
return 0;
}
#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_H264(rw, name) FUNC_NAME(rw, h264, name)
#define READ
#define READWRITE read
#define RWContext BitstreamContext
#define xu(width, name, var, range_min, range_max) do { \
uint32_t value = range_min; \
CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
&value, range_min, range_max)); \
var = value; \
} while (0)
#define xue(name, var, range_min, range_max) do { \
uint32_t value = range_min; \
CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
&value, range_min, range_max)); \
var = value; \
} while (0)
#define xse(name, var, range_min, range_max) do { \
int32_t value = range_min; \
CHECK(cbs_read_se_golomb(ctx, rw, #name, \
&value, range_min, range_max)); \
var = value; \
} while (0)
#define u(width, name, range_min, range_max) \
xu(width, name, current->name, range_min, range_max)
#define flag(name) u(1, name, 0, 1)
#define ue(name, range_min, range_max) \
xue(name, current->name, range_min, range_max)
#define se(name, range_min, range_max) \
xse(name, current->name, range_min, range_max)
#define infer(name, value) do { \
current->name = value; \
} while (0)
static int cbs_h2645_read_more_rbsp_data(BitstreamContext *bc)
{
int bits_left = bitstream_bits_left(bc);
if (bits_left > 8)
return 1;
if (bitstream_peek(bc, bits_left) == 1 << (bits_left - 1))
return 0;
return 1;
}
#define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
#define byte_alignment(rw) (bitstream_tell(rw) % 8)
#define allocate(name, size) do { \
name = av_mallocz(size); \
if (!name) \
return AVERROR(ENOMEM); \
} while (0)
#define FUNC(name) FUNC_H264(READWRITE, name)
#include "cbs_h264_syntax_template.c"
#undef FUNC
#undef READ
#undef READWRITE
#undef RWContext
#undef xu
#undef xue
#undef xse
#undef u
#undef flag
#undef ue
#undef se
#undef infer
#undef more_rbsp_data
#undef byte_alignment
#undef allocate
#define WRITE
#define READWRITE write
#define RWContext PutBitContext
#define xu(width, name, var, range_min, range_max) do { \
uint32_t value = var; \
CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
value, range_min, range_max)); \
} while (0)
#define xue(name, var, range_min, range_max) do { \
uint32_t value = var; \
CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
value, range_min, range_max)); \
} while (0)
#define xse(name, var, range_min, range_max) do { \
int32_t value = var; \
CHECK(cbs_write_se_golomb(ctx, rw, #name, \
value, range_min, range_max)); \
} while (0)
#define u(width, name, range_min, range_max) \
xu(width, name, current->name, range_min, range_max)
#define flag(name) u(1, name, 0, 1)
#define ue(name, range_min, range_max) \
xue(name, current->name, range_min, range_max)
#define se(name, range_min, range_max) \
xse(name, current->name, range_min, range_max)
#define infer(name, value) do { \
if (current->name != (value)) { \
av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
"%s does not match inferred value: " \
"%"PRId64", but should be %"PRId64".\n", \
#name, (int64_t)current->name, (int64_t)(value)); \
} \
} while (0)
#define more_rbsp_data(var) (var)
#define byte_alignment(rw) (put_bits_count(rw) % 8)
#define allocate(name, size) do { \
if (!name) { \
av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
"for writing.\n", #name); \
return AVERROR_INVALIDDATA; \
} \
} while (0)
#define FUNC(name) FUNC_H264(READWRITE, name)
#include "cbs_h264_syntax_template.c"
#undef FUNC
#undef WRITE
#undef READWRITE
#undef RWContext
#undef xu
#undef xue
#undef xse
#undef u
#undef flag
#undef ue
#undef se
#undef infer
#undef more_rbsp_data
#undef byte_alignment
#undef allocate
static void cbs_h264_free_sei(H264RawSEI *sei)
{
int i;
for (i = 0; i < sei->payload_count; i++) {
H264RawSEIPayload *payload = &sei->payload[i];
switch (payload->payload_type) {
case H264_SEI_TYPE_BUFFERING_PERIOD:
case H264_SEI_TYPE_PIC_TIMING:
case H264_SEI_TYPE_RECOVERY_POINT:
case H264_SEI_TYPE_DISPLAY_ORIENTATION:
break;
case H264_SEI_TYPE_USER_DATA_REGISTERED:
av_freep(&payload->payload.user_data_registered.data);
break;
case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
av_freep(&payload->payload.user_data_unregistered.data);
break;
default:
av_freep(&payload->payload.other.data);
break;
}
}
}
static void cbs_h264_free_slice(H264RawSlice *slice)
{
av_freep(&slice->data);
}
static void cbs_h264_free_nal_unit(CodedBitstreamUnit *unit)
{
switch (unit->type) {
case H264_NAL_SEI:
cbs_h264_free_sei(unit->content);
break;
case H264_NAL_IDR_SLICE:
case H264_NAL_SLICE:
cbs_h264_free_slice(unit->content);
break;
}
av_freep(&unit->content);
}
static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
const H2645Packet *packet)
{
int err, i;
for (i = 0; i < packet->nb_nals; i++) {
const H2645NAL *nal = &packet->nals[i];
uint8_t *data;
data = av_malloc(nal->size);
if (!data)
return AVERROR(ENOMEM);
memcpy(data, nal->data, nal->size);
err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
data, nal->size);
if (err < 0) {
av_freep(&data);
return err;
}
}
return 0;
}
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
int header)
{
enum AVCodecID codec_id = ctx->codec->codec_id;
CodedBitstreamH2645Context *priv = ctx->priv_data;
GetByteContext gbc;
int err;
av_assert0(frag->data && frag->nb_units == 0);
if (frag->data_size == 0)
return 0;
if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
// AVCC header.
size_t size, start, end;
int i, count, version;
priv->mp4 = 1;
bytestream2_init(&gbc, frag->data, frag->data_size);
if (bytestream2_get_bytes_left(&gbc) < 6)
return AVERROR_INVALIDDATA;
version = bytestream2_get_byte(&gbc);
if (version != 1) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
"first byte %u.", version);
return AVERROR_INVALIDDATA;
}
bytestream2_skip(&gbc, 3);
priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
// SPS array.
count = bytestream2_get_byte(&gbc) & 0x1f;
start = bytestream2_tell(&gbc);
for (i = 0; i < count; i++) {
if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
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_H264);
if (err < 0) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
return err;
}
err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
if (err < 0)
return err;
// PPS array.
count = bytestream2_get_byte(&gbc);
start = bytestream2_tell(&gbc);
for (i = 0; i < count; i++) {
if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
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_H264);
if (err < 0) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
return err;
}
err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
if (err < 0)
return err;
if (bytestream2_get_bytes_left(&gbc) > 0) {
av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
"header.\n", bytestream2_get_bytes_left(&gbc));
}
} else {
// Annex B, or later MP4 with already-known parameters.
err = ff_h2645_packet_split(&priv->read_packet,
frag->data, frag->data_size,
ctx->log_ctx,
priv->mp4, priv->nal_length_size,
codec_id);
if (err < 0)
return err;
err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
if (err < 0)
return err;
}
return 0;
}
#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
const H26 ## h26n ## Raw ## ps_name *ps_var) \
{ \
CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
unsigned int id = ps_var->id_element; \
if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
" id : %d.\n", id); \
return AVERROR_INVALIDDATA; \
} \
av_freep(&priv->ps_var[id]); \
priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
if (!priv->ps_var[id]) \
return AVERROR(ENOMEM); \
memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
return 0; \
}
cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit)
{
BitstreamContext bc;
int err;
err = bitstream_init(&bc, unit->data, 8 * unit->data_size);
if (err < 0)
return err;
switch (unit->type) {
case H264_NAL_SPS:
{
H264RawSPS *sps;
sps = av_mallocz(sizeof(*sps));
if (!sps)
return AVERROR(ENOMEM);
err = cbs_h264_read_sps(ctx, &bc, sps);
if (err >= 0)
err = cbs_h264_replace_sps(ctx, sps);
if (err < 0) {
av_free(sps);
return err;
}
unit->content = sps;
}
break;
case H264_NAL_SPS_EXT:
{
H264RawSPSExtension *sps_ext;
sps_ext = av_mallocz(sizeof(*sps_ext));
if (!sps_ext)
return AVERROR(ENOMEM);
err = cbs_h264_read_sps_extension(ctx, &bc, sps_ext);
if (err < 0) {
av_free(sps_ext);
return err;
}
unit->content = sps_ext;
}
break;
case H264_NAL_PPS:
{
H264RawPPS *pps;
pps = av_mallocz(sizeof(*pps));
if (!pps)
return AVERROR(ENOMEM);
err = cbs_h264_read_pps(ctx, &bc, pps);
if (err >= 0)
err = cbs_h264_replace_pps(ctx, pps);
if (err < 0) {
av_free(pps);
return err;
}
unit->content = pps;
}
break;
case H264_NAL_SLICE:
case H264_NAL_IDR_SLICE:
case H264_NAL_AUXILIARY_SLICE:
{
H264RawSlice *slice;
int pos, len;
slice = av_mallocz(sizeof(*slice));
if (!slice)
return AVERROR(ENOMEM);
err = cbs_h264_read_slice_header(ctx, &bc, &slice->header);
if (err < 0) {
av_free(slice);
return err;
}
pos = bitstream_tell(&bc);
len = unit->data_size;
if (!unit->data[len - 1]) {
int z;
for (z = 0; z < len && !unit->data[len - z - 1]; z++);
av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
"from slice data.\n", z);
len -= z;
}
slice->data_size = len - pos / 8;
slice->data = av_malloc(slice->data_size);
if (!slice->data) {
av_free(slice);
return AVERROR(ENOMEM);
}
memcpy(slice->data,
unit->data + pos / 8, slice->data_size);
slice->data_bit_start = pos % 8;
unit->content = slice;
}
break;
case H264_NAL_AUD:
{
H264RawAUD *aud;
aud = av_mallocz(sizeof(*aud));
if (!aud)
return AVERROR(ENOMEM);
err = cbs_h264_read_aud(ctx, &bc, aud);
if (err < 0) {
av_free(aud);
return err;
}
unit->content = aud;
}
break;
case H264_NAL_SEI:
{
H264RawSEI *sei;
sei = av_mallocz(sizeof(*sei));
if (!sei)
return AVERROR(ENOMEM);
err = cbs_h264_read_sei(ctx, &bc, sei);
if (err < 0) {
cbs_h264_free_sei(sei);
return err;
}
unit->content = sei;
}
break;
default:
return AVERROR(ENOSYS);
}
return 0;
}
static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit,
PutBitContext *pbc)
{
int err;
switch (unit->type) {
case H264_NAL_SPS:
{
H264RawSPS *sps = unit->content;
err = cbs_h264_write_sps(ctx, pbc, sps);
if (err < 0)
return err;
err = cbs_h264_replace_sps(ctx, sps);
if (err < 0)
return err;
}
break;
case H264_NAL_SPS_EXT:
{
H264RawSPSExtension *sps_ext;
err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
if (err < 0)
return err;
}
break;
case H264_NAL_PPS:
{
H264RawPPS *pps = unit->content;
err = cbs_h264_write_pps(ctx, pbc, pps);
if (err < 0)
return err;
err = cbs_h264_replace_pps(ctx, pps);
if (err < 0)
return err;
}
break;
case H264_NAL_SLICE:
case H264_NAL_IDR_SLICE:
{
H264RawSlice *slice = unit->content;
BitstreamContext bc;
int bits_left, end, zeroes;
err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
if (err < 0)
return err;
if (slice->data) {
if (slice->data_size * 8 + 8 > put_bits_left(pbc))
return AVERROR(ENOSPC);
bitstream_init(&bc, slice->data, slice->data_size * 8);
bitstream_skip(&bc, slice->data_bit_start);
// Copy in two-byte blocks, but stop before copying the
// rbsp_stop_one_bit in the final byte.
while (bitstream_bits_left(&bc) > 23)
put_bits(pbc, 16, bitstream_read(&bc, 16));
bits_left = bitstream_bits_left(&bc);
end = bitstream_read(&bc, bits_left);
// rbsp_stop_one_bit must be present here.
av_assert0(end);
zeroes = ff_ctz(end);
if (bits_left > zeroes + 1)
put_bits(pbc, bits_left - zeroes - 1,
end >> (zeroes + 1));
put_bits(pbc, 1, 1);
while (put_bits_count(pbc) % 8 != 0)
put_bits(pbc, 1, 0);
} else {
// No slice data - that was just the header.
// (Bitstream may be unaligned!)
}
}
break;
case H264_NAL_AUD:
{
err = cbs_h264_write_aud(ctx, pbc, unit->content);
if (err < 0)
return err;
}
break;
case H264_NAL_SEI:
{
err = cbs_h264_write_sei(ctx, pbc, unit->content);
if (err < 0)
return err;
}
break;
default:
av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
"NAL unit type %"PRIu32".\n", unit->type);
return AVERROR_PATCHWELCOME;
}
return 0;
}
static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit)
{
CodedBitstreamH2645Context *priv = ctx->priv_data;
enum AVCodecID codec_id = ctx->codec->codec_id;
PutBitContext pbc;
int err;
if (!priv->write_buffer) {
// Initial write buffer size is 1MB.
priv->write_buffer_size = 1024 * 1024;
reallocate_and_try_again:
err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
if (err < 0) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
"sufficiently large write buffer (last attempt "
"%zu bytes).\n", priv->write_buffer_size);
return err;
}
}
init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
if (err == AVERROR(ENOSPC)) {
// Overflow.
priv->write_buffer_size *= 2;
goto reallocate_and_try_again;
}
// Overflow but we didn't notice.
av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
if (put_bits_count(&pbc) % 8)
unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
else
unit->data_bit_padding = 0;
unit->data_size = (put_bits_count(&pbc) + 7) / 8;
flush_put_bits(&pbc);
err = av_reallocp(&unit->data, unit->data_size);
if (err < 0)
return err;
memcpy(unit->data, priv->write_buffer, unit->data_size);
return 0;
}
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag)
{
uint8_t *data;
size_t max_size, dp, sp;
int err, i, zero_run;
for (i = 0; i < frag->nb_units; i++) {
// Data should already all have been written when we get here.
av_assert0(frag->units[i].data);
}
max_size = 0;
for (i = 0; i < frag->nb_units; i++) {
// Start code + content with worst-case emulation prevention.
max_size += 3 + frag->units[i].data_size * 3 / 2;
}
data = av_malloc(max_size);
if (!data)
return AVERROR(ENOMEM);
dp = 0;
for (i = 0; i < frag->nb_units; i++) {
CodedBitstreamUnit *unit = &frag->units[i];
if (unit->data_bit_padding > 0) {
if (i < frag->nb_units - 1)
av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
"unaligned padding on non-final NAL unit.\n");
else
frag->data_bit_padding = unit->data_bit_padding;
}
if (unit->type == H264_NAL_SPS ||
unit->type == H264_NAL_PPS ||
i == 0 /* (Assume this is the start of an access unit.) */) {
// zero_byte
data[dp++] = 0;
}
// start_code_prefix_one_3bytes
data[dp++] = 0;
data[dp++] = 0;
data[dp++] = 1;
zero_run = 0;
for (sp = 0; sp < unit->data_size; sp++) {
if (zero_run < 2) {
if (unit->data[sp] == 0)
++zero_run;
else
zero_run = 0;
} else {
if ((unit->data[sp] & ~3) == 0) {
// emulation_prevention_three_byte
data[dp++] = 3;
}
zero_run = unit->data[sp] == 0;
}
data[dp++] = unit->data[sp];
}
}
av_assert0(dp <= max_size);
err = av_reallocp(&data, dp);
if (err)
return err;
frag->data = data;
frag->data_size = dp;
return 0;
}
static void cbs_h264_close(CodedBitstreamContext *ctx)
{
CodedBitstreamH264Context *h264 = ctx->priv_data;
int i;
ff_h2645_packet_uninit(&h264->common.read_packet);
av_freep(&h264->common.write_buffer);
for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
av_freep(&h264->sps[i]);
for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
av_freep(&h264->pps[i]);
}
const CodedBitstreamType ff_cbs_type_h264 = {
.codec_id = AV_CODEC_ID_H264,
.priv_data_size = sizeof(CodedBitstreamH264Context),
.split_fragment = &cbs_h2645_split_fragment,
.read_unit = &cbs_h264_read_nal_unit,
.write_unit = &cbs_h2645_write_nal_unit,
.assemble_fragment = &cbs_h2645_assemble_fragment,
.free_unit = &cbs_h264_free_nal_unit,
.close = &cbs_h264_close,
};

43
libavcodec/cbs_h2645.h Normal file
View File

@ -0,0 +1,43 @@
/*
* 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_H2645_H
#define AVCODEC_CBS_H2645_H
#include <stddef.h>
#include <stdint.h>
#include "h2645_parse.h"
typedef struct CodedBitstreamH2645Context {
// If set, the stream being read is in MP4 (AVCC/HVCC) format. If not
// set, the stream is assumed to be in annex B format.
int mp4;
// Size in bytes of the NAL length field for MP4 format.
int nal_length_size;
// Packet reader.
H2645Packet read_packet;
// Write buffer
uint8_t *write_buffer;
size_t write_buffer_size;
} CodedBitstreamH2645Context;
#endif /* AVCODEC_CBS_H2645_H */

File diff suppressed because it is too large Load Diff

View File

@ -80,4 +80,7 @@ int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
uint32_t range_min, uint32_t range_max);
extern const CodedBitstreamType ff_cbs_type_h264;
#endif /* AVCODEC_CBS_INTERNAL_H */