From 30645174e333c7db1946ab09be769db9be9d7481 Mon Sep 17 00:00:00 2001 From: Mark Thompson Date: Sat, 9 Sep 2017 22:49:14 +0100 Subject: [PATCH 1/6] vaapi_h264: Fix CPB/DPB delays This should be ticks, not time_scale steps - it was wrong for all framerates not a multiple of 1/2. --- libavcodec/vaapi_encode_h264.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c index 271644ebbe..1288249be7 100644 --- a/libavcodec/vaapi_encode_h264.c +++ b/libavcodec/vaapi_encode_h264.c @@ -612,10 +612,8 @@ static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx, if (opt->sei & SEI_TIMING) { memset(&priv->pic_timing, 0, sizeof(priv->pic_timing)); - priv->pic_timing.cpb_removal_delay = - 2 * sps->vui.num_units_in_tick * priv->cpb_delay; - priv->pic_timing.dpb_output_delay = - 2 * sps->vui.num_units_in_tick * priv->dpb_delay; + priv->pic_timing.cpb_removal_delay = 2 * priv->cpb_delay; + priv->pic_timing.dpb_output_delay = 2 * priv->dpb_delay; priv->sei_needed = 1; } From f763489364416bb6866adc4f4a96012dd2ca1bd0 Mon Sep 17 00:00:00 2001 From: Mark Thompson Date: Mon, 28 Aug 2017 17:06:14 +0100 Subject: [PATCH 2/6] cbs_h265: Fix reading of unknown parameter set extension data --- libavcodec/cbs_h265_syntax_template.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavcodec/cbs_h265_syntax_template.c b/libavcodec/cbs_h265_syntax_template.c index 40fdaf8a90..8564220d53 100644 --- a/libavcodec/cbs_h265_syntax_template.c +++ b/libavcodec/cbs_h265_syntax_template.c @@ -67,7 +67,8 @@ static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw, BitstreamContext start; uint8_t bit; start = *rw; - for (k = 0; cbs_h2645_read_more_rbsp_data(rw); k++); + for (k = 0; cbs_h2645_read_more_rbsp_data(rw); k++) + bitstream_skip(rw, 1); current->bit_length = k; if (k > 0) { *rw = start; From 067a9ddeb8feff1f724856f0054930c55219f76b Mon Sep 17 00:00:00 2001 From: Mark Thompson Date: Mon, 28 Aug 2017 17:07:01 +0100 Subject: [PATCH 3/6] cbs_h265: Fix ranges of prediction weight offsets The bracketing was wrong - '-' binds before '<<'. This would previously incorrectly reject the streams in the WP_A and WP_B conformance tests. --- libavcodec/cbs_h265_syntax_template.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libavcodec/cbs_h265_syntax_template.c b/libavcodec/cbs_h265_syntax_template.c index 8564220d53..a194887d76 100644 --- a/libavcodec/cbs_h265_syntax_template.c +++ b/libavcodec/cbs_h265_syntax_template.c @@ -1133,7 +1133,7 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw, se(delta_luma_weight_l0[i], -128, +127); se(luma_offset_l0[i], -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)), - +(1 << (sps->bit_depth_luma_minus8 + 8 - 1) - 1)); + ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1)); } else { infer(delta_luma_weight_l0[i], 0); infer(luma_offset_l0[i], 0); @@ -1143,7 +1143,7 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw, se(delta_chroma_weight_l0[i][j], -128, +127); se(chroma_offset_l0[i][j], -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)), - +(4 << (sps->bit_depth_chroma_minus8 + 8 - 1) - 1)); + ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1)); } } else { for (j = 0; j < 2; j++) { @@ -1173,8 +1173,8 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw, if (current->luma_weight_l1_flag[i]) { se(delta_luma_weight_l1[i], -128, +127); se(luma_offset_l1[i], - - 1 << (sps->bit_depth_luma_minus8 + 8 - 1), - + 1 << (sps->bit_depth_luma_minus8 + 8 - 1) - 1); + -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)), + ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1)); } else { infer(delta_luma_weight_l1[i], 0); infer(luma_offset_l1[i], 0); @@ -1183,8 +1183,8 @@ static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw, for (j = 0; j < 2; j++) { se(delta_chroma_weight_l1[i][j], -128, +127); se(chroma_offset_l1[i][j], - - 4 << (sps->bit_depth_chroma_minus8 + 8 - 1), - + 4 << (sps->bit_depth_chroma_minus8 + 8 - 1) - 1); + -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)), + ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1)); } } else { for (j = 0; j < 2; j++) { From a41b69b5eb950c10d8ede472bcc4e88ce4246db9 Mon Sep 17 00:00:00 2001 From: Mark Thompson Date: Sat, 9 Sep 2017 23:30:32 +0100 Subject: [PATCH 4/6] cbs_mpeg2: Add support for picture display extension --- libavcodec/cbs_mpeg2.h | 8 +++++ libavcodec/cbs_mpeg2_syntax_template.c | 44 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/libavcodec/cbs_mpeg2.h b/libavcodec/cbs_mpeg2.h index 32c4d4e2b1..0b6cb998dc 100644 --- a/libavcodec/cbs_mpeg2.h +++ b/libavcodec/cbs_mpeg2.h @@ -160,6 +160,11 @@ typedef struct MPEG2RawQuantMatrixExtension { uint8_t chroma_non_intra_quantiser_matrix[64]; } MPEG2RawQuantMatrixExtension; +typedef struct MPEG2RawPictureDisplayExtension { + uint16_t frame_centre_horizontal_offset[3]; + uint16_t frame_centre_vertical_offset[3]; +} MPEG2RawPictureDisplayExtension; + typedef struct MPEG2RawExtensionData { uint8_t extension_start_code; uint8_t extension_start_code_identifier; @@ -169,6 +174,7 @@ typedef struct MPEG2RawExtensionData { MPEG2RawSequenceDisplayExtension sequence_display; MPEG2RawQuantMatrixExtension quant_matrix; MPEG2RawPictureCodingExtension picture_coding; + MPEG2RawPictureDisplayExtension picture_display; } data; } MPEG2RawExtensionData; @@ -206,6 +212,8 @@ typedef struct CodedBitstreamMPEG2Context { uint16_t vertical_size; uint8_t scalable; uint8_t scalable_mode; + uint8_t progressive_sequence; + uint8_t number_of_frame_centre_offsets; // Write buffer. uint8_t *write_buffer; diff --git a/libavcodec/cbs_mpeg2_syntax_template.c b/libavcodec/cbs_mpeg2_syntax_template.c index b6dd42d2ee..4aa1eb3c06 100644 --- a/libavcodec/cbs_mpeg2_syntax_template.c +++ b/libavcodec/cbs_mpeg2_syntax_template.c @@ -101,6 +101,7 @@ static int FUNC(sequence_extension)(CodedBitstreamContext *ctx, RWContext *rw, current->horizontal_size_extension << 12; mpeg2->vertical_size = (mpeg2->vertical_size & 0xfff) | current->vertical_size_extension << 12; + mpeg2->progressive_sequence = current->progressive_sequence; ui(12, bit_rate_extension); marker_bit(); @@ -183,6 +184,7 @@ static int FUNC(picture_header)(CodedBitstreamContext *ctx, RWContext *rw, static int FUNC(picture_coding_extension)(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawPictureCodingExtension *current) { + CodedBitstreamMPEG2Context *mpeg2 = ctx->priv_data; int err; HEADER("Picture Coding Extension"); @@ -204,6 +206,27 @@ static int FUNC(picture_coding_extension)(CodedBitstreamContext *ctx, RWContext ui(1, chroma_420_type); ui(1, progressive_frame); + if (mpeg2->progressive_sequence) { + if (current->repeat_first_field) { + if (current->top_field_first) + mpeg2->number_of_frame_centre_offsets = 3; + else + mpeg2->number_of_frame_centre_offsets = 2; + } else { + mpeg2->number_of_frame_centre_offsets = 1; + } + } else { + if (current->picture_structure == 1 || // Top field. + current->picture_structure == 2) { // Bottom field. + mpeg2->number_of_frame_centre_offsets = 1; + } else { + if (current->repeat_first_field) + mpeg2->number_of_frame_centre_offsets = 3; + else + mpeg2->number_of_frame_centre_offsets = 2; + } + } + ui(1, composite_display_flag); if (current->composite_display_flag) { ui(1, v_axis); @@ -250,6 +273,24 @@ static int FUNC(quant_matrix_extension)(CodedBitstreamContext *ctx, RWContext *r return 0; } +static int FUNC(picture_display_extension)(CodedBitstreamContext *ctx, RWContext *rw, + MPEG2RawPictureDisplayExtension *current) +{ + CodedBitstreamMPEG2Context *mpeg2 = ctx->priv_data; + int err, i; + + HEADER("Picture Display Extension"); + + for (i = 0; i < mpeg2->number_of_frame_centre_offsets; i++) { + ui(16, frame_centre_horizontal_offset[i]); + marker_bit(); + ui(16, frame_centre_vertical_offset[i]); + marker_bit(); + } + + return 0; +} + static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawExtensionData *current) { @@ -270,6 +311,9 @@ static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw, case 3: return FUNC(quant_matrix_extension) (ctx, rw, ¤t->data.quant_matrix); + case 7: + return FUNC(picture_display_extension) + (ctx, rw, ¤t->data.picture_display); case 8: return FUNC(picture_coding_extension) (ctx, rw, ¤t->data.picture_coding); From b5859e0b04bdbe12c97cb12ac10a45d51d2d73c9 Mon Sep 17 00:00:00 2001 From: Mark Thompson Date: Tue, 12 Sep 2017 22:11:56 +0100 Subject: [PATCH 5/6] mpeg12: Move finding the best frame rate to common code Previously in the mpeg2_metadata filter. Also adds a test. --- libavcodec/Makefile | 1 + libavcodec/mpeg12.h | 4 ++ libavcodec/mpeg12framerate.c | 64 ++++++++++++++++++++++ libavcodec/mpeg2_metadata_bsf.c | 58 ++------------------ libavcodec/tests/mpeg12framerate.c | 87 ++++++++++++++++++++++++++++++ tests/fate/libavcodec.mak | 5 ++ 6 files changed, 166 insertions(+), 53 deletions(-) create mode 100644 libavcodec/tests/mpeg12framerate.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 9c485b222e..c750abd2d3 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -824,6 +824,7 @@ TESTPROGS-$(CONFIG_FFT) += fft fft-fixed TESTPROGS-$(CONFIG_GOLOMB) += golomb TESTPROGS-$(CONFIG_IDCTDSP) += dct TESTPROGS-$(CONFIG_IIRFILTER) += iirfilter +TESTPROGS-$(CONFIG_MPEGVIDEO) += mpeg12framerate TESTPROGS-$(CONFIG_RANGECODER) += rangecoder TESTOBJS = dctref.o diff --git a/libavcodec/mpeg12.h b/libavcodec/mpeg12.h index 17f0b781e6..26de7a48a8 100644 --- a/libavcodec/mpeg12.h +++ b/libavcodec/mpeg12.h @@ -64,4 +64,8 @@ void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64], void ff_mpeg1_encode_init(MpegEncContext *s); void ff_mpeg1_encode_slice_header(MpegEncContext *s); +void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, + int *code, int *ext_n, int *ext_d, + int nonstandard); + #endif /* AVCODEC_MPEG12_H */ diff --git a/libavcodec/mpeg12framerate.c b/libavcodec/mpeg12framerate.c index b11dd5e01d..acfef09745 100644 --- a/libavcodec/mpeg12framerate.c +++ b/libavcodec/mpeg12framerate.c @@ -18,6 +18,9 @@ #include "libavutil/rational.h" +#include "mpeg12.h" +#include "mpeg12data.h" + const AVRational ff_mpeg12_frame_rate_tab[16] = { { 0, 0}, {24000, 1001}, @@ -37,3 +40,64 @@ const AVRational ff_mpeg12_frame_rate_tab[16] = { { 15, 1}, { 0, 0}, }; + +void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, + int *code, int *ext_n, int *ext_d, + int nonstandard) +{ + int mpeg2 = ext_n && ext_d; + int max_code = nonstandard ? 12 : 8; + int c, n, d, best_c, best_n, best_d; + AVRational best_error = { INT_MAX, 1 }; + + // Default to NTSC if the inputs make no sense. + best_c = 4; + best_n = best_d = 1; + + for (c = 1; c <= max_code; c++) { + if (av_cmp_q(frame_rate, ff_mpeg12_frame_rate_tab[c]) == 0) { + best_c = c; + goto found; + } + } + + for (c = 1; c <= max_code; c++) { + for (n = 1; n <= (mpeg2 ? 4 : 1); n++) { + for (d = 1; d <= (mpeg2 ? 32 : 1); d++) { + AVRational test, error; + int cmp; + + test = av_mul_q(ff_mpeg12_frame_rate_tab[c], + (AVRational) { n, d }); + + cmp = av_cmp_q(test, frame_rate); + if (cmp == 0) { + best_c = c; + best_n = n; + best_d = d; + goto found; + } + + if (cmp < 0) + error = av_div_q(frame_rate, test); + else + error = av_div_q(test, frame_rate); + + cmp = av_cmp_q(error, best_error); + if (cmp < 0 || (cmp == 0 && n == 1 && d == 1)) { + best_c = c; + best_n = n; + best_d = d; + best_error = error; + } + } + } + } + +found: + *code = best_c; + if (mpeg2) { + *ext_n = best_n - 1; + *ext_d = best_d - 1; + } +} diff --git a/libavcodec/mpeg2_metadata_bsf.c b/libavcodec/mpeg2_metadata_bsf.c index 80e946f58e..5dae481d3d 100644 --- a/libavcodec/mpeg2_metadata_bsf.c +++ b/libavcodec/mpeg2_metadata_bsf.c @@ -23,6 +23,7 @@ #include "bsf.h" #include "cbs.h" #include "cbs_mpeg2.h" +#include "mpeg12.h" typedef struct MPEG2MetadataContext { const AVClass *class; @@ -99,63 +100,14 @@ static int mpeg2_metadata_update_fragment(AVBSFContext *bsf, } if (ctx->frame_rate.num && ctx->frame_rate.den) { - // Table 6-4. - static AVRational frame_rate_table[] = { - { 0, 0 }, - { 24000, 1001 }, - { 24, 1 }, - { 25, 1 }, - { 30000, 1001 }, - { 30, 1 }, - { 50, 1 }, - { 60000, 1001 }, - { 60, 1 }, - }; int code, ext_n, ext_d; - AVRational best_error = { INT_MAX, 1 }; - - for (i = 1; i < FF_ARRAY_ELEMS(frame_rate_table); i++) { - if (av_cmp_q(ctx->frame_rate, frame_rate_table[i]) == 0) { - code = i; - ext_n = 1; - ext_d = 1; - goto found_frame_rate; - } - } - for (i = 1; i < FF_ARRAY_ELEMS(frame_rate_table); i++) { - AVRational fr, error; - int n, d, cmp; - for (n = 1; n <= 4; n++) { - for (d = 1; d <= 32; d++) { - fr = av_mul_q(frame_rate_table[i], - (AVRational) { n, d }); - cmp = av_cmp_q(fr, ctx->frame_rate); - if (cmp == 0) { - code = i; - ext_n = n; - ext_d = d; - goto found_frame_rate; - } - if (cmp < 0) - error = av_div_q(ctx->frame_rate, fr); - else - error = av_div_q(fr, ctx->frame_rate); - cmp = av_cmp_q(error, best_error); - if (cmp < 0 || (cmp == 0 && n == 1 && d == 1)) { - code = i; - ext_n = n; - ext_d = d; - best_error = error; - } - } - } - } + ff_mpeg12_find_best_frame_rate(ctx->frame_rate, + &code, &ext_n, &ext_d, 0); - found_frame_rate: sh->frame_rate_code = code; - se->frame_rate_extension_n = ext_n - 1; - se->frame_rate_extension_d = ext_d - 1; + se->frame_rate_extension_n = ext_n; + se->frame_rate_extension_d = ext_d; } if (ctx->video_format >= 0 || diff --git a/libavcodec/tests/mpeg12framerate.c b/libavcodec/tests/mpeg12framerate.c new file mode 100644 index 0000000000..c493cd0f5b --- /dev/null +++ b/libavcodec/tests/mpeg12framerate.c @@ -0,0 +1,87 @@ +/* + * 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 "libavcodec/mpeg12.h" +#include "libavcodec/mpeg12data.h" + +int main(void) +{ + int i; + +#define TEST_MATCH(frame_rate, code, ext_n, ext_d) do { \ + AVRational fr = frame_rate; \ + int c, n, d; \ + ff_mpeg12_find_best_frame_rate(fr, &c, &n, &d, 0); \ + if (c != code || n != ext_n || d != ext_d) { \ + av_log(NULL, AV_LOG_ERROR, "Failed to match %d/%d: " \ + "code = %d, ext_n = %d, ext_d = %d.\n", \ + fr.num, fr.den, c, n, d); \ + return 1; \ + } \ + } while (0) +#define TEST_EXACT(frn, frd) do { \ + AVRational fr = (AVRational) { frn, frd }; \ + int c, n, d; \ + ff_mpeg12_find_best_frame_rate(fr, &c, &n, &d, 0); \ + if (av_cmp_q(fr, av_mul_q(ff_mpeg12_frame_rate_tab[c], \ + (AVRational) { n + 1, d + 1 })) != 0) { \ + av_log(NULL, AV_LOG_ERROR, "Failed to find exact %d/%d: " \ + "code = %d, ext_n = %d, ext_d = %d.\n", \ + fr.num, fr.den, c, n, d); \ + return 1; \ + } \ + } while (0) + + // Framerates in the table must be chosen exactly. + for (i = 1; i <= 8; i++) + TEST_MATCH(ff_mpeg12_frame_rate_tab[i], i, 0, 0); + + // As should the same ones with small perturbations. + // (1/1000 used here to be smaller than half the difference + // between 24 and 24000/1001.) + for (i = 1; i <= 8; i++) { + TEST_MATCH(av_sub_q(ff_mpeg12_frame_rate_tab[i], + (AVRational) { 1, 1000 }), i, 0, 0); + TEST_MATCH(av_add_q(ff_mpeg12_frame_rate_tab[i], + (AVRational) { 1, 1000 }), i, 0, 0); + } + + // Exactly constructable framerates should be exact. Note that some + // values can be made in multiple ways (e.g. 12 = 24 / 2 == 60 / 5), + // and there is no reason to favour any particular choice. + TEST_EXACT( 1, 1); + TEST_EXACT( 2, 1); + TEST_EXACT( 12, 1); + TEST_EXACT( 15000, 1001); + TEST_EXACT( 15, 1); + TEST_EXACT( 120, 1); + TEST_EXACT(120000, 1001); + TEST_EXACT( 200, 1); + TEST_EXACT( 240, 1); + + // Values higher than 240 (the highest representable, as 60 * 4 / 1) + // should be mapped to 240. + for (i = 240; i < 1000; i += 10) + TEST_MATCH(((AVRational) { i, 1 }), 8, 3, 0); + // Values lower than 24000/32032 (the lowest representable, as + // 24000/1001 * 1 / 32) should be mapped to 24000/32032. + for (i = 74; i > 0; i--) + TEST_MATCH(((AVRational) { i, 100 }), 1, 0, 31); + + return 0; +} diff --git a/tests/fate/libavcodec.mak b/tests/fate/libavcodec.mak index 0bbe57223f..257016261d 100644 --- a/tests/fate/libavcodec.mak +++ b/tests/fate/libavcodec.mak @@ -12,6 +12,11 @@ FATE_LIBAVCODEC-$(CONFIG_IIRFILTER) += fate-iirfilter fate-iirfilter: libavcodec/tests/iirfilter$(EXESUF) fate-iirfilter: CMD = run libavcodec/tests/iirfilter +FATE_LIBAVCODEC-$(CONFIG_MPEGVIDEO) += fate-mpeg12framerate +fate-mpeg12framerate: libavcodec/tests/mpeg12framerate$(EXESUF) +fate-mpeg12framerate: CMD = run libavcodec/tests/mpeg12framerate +fate-mpeg12framerate: CMP = null + FATE_LIBAVCODEC-$(CONFIG_RANGECODER) += fate-rangecoder fate-rangecoder: libavcodec/tests/rangecoder$(EXESUF) fate-rangecoder: CMD = run libavcodec/tests/rangecoder From 10eb496d9ae94df6f792b0e1d8750738eb3a0952 Mon Sep 17 00:00:00 2001 From: Mark Thompson Date: Sat, 24 Jun 2017 00:29:32 +0100 Subject: [PATCH 6/6] vaapi_mpeg2: Convert to use coded bitstream infrastructure --- configure | 2 +- libavcodec/vaapi_encode_mpeg2.c | 638 +++++++++++++++++++++----------- 2 files changed, 424 insertions(+), 216 deletions(-) diff --git a/configure b/configure index c24cdd4f7e..7728cd6de6 100755 --- a/configure +++ b/configure @@ -2301,7 +2301,7 @@ mpeg2_mmal_decoder_deps="mmal" mpeg2_qsv_decoder_select="qsvdec mpeg2_qsv_hwaccel mpegvideo_parser" mpeg2_qsv_encoder_select="qsvenc" mpeg2_vaapi_encoder_deps="VAEncPictureParameterBufferMPEG2" -mpeg2_vaapi_encoder_select="vaapi_encode" +mpeg2_vaapi_encoder_select="cbs_mpeg2 vaapi_encode" mpeg4_omx_encoder_deps="omx" vc1_mmal_decoder_deps="mmal" vc1_qsv_decoder_select="qsvdec vc1_qsv_hwaccel vc1_parser" diff --git a/libavcodec/vaapi_encode_mpeg2.c b/libavcodec/vaapi_encode_mpeg2.c index 634178521f..8b956eb3e0 100644 --- a/libavcodec/vaapi_encode_mpeg2.c +++ b/libavcodec/vaapi_encode_mpeg2.c @@ -20,15 +20,11 @@ #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 "internal.h" -#include "mpegvideo.h" -#include "put_bits.h" +#include "cbs.h" +#include "cbs_mpeg2.h" +#include "mpeg12.h" #include "vaapi_encode.h" typedef struct VAAPIEncodeMPEG2Context { @@ -39,79 +35,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(32, GOP_START_CODE, group_start_code); - u(25, vgop_field(time_code)); - u(1, vgop_field(closed_gop)); - u(1, vgop_field(broken_link)); - - while (put_bits_count(&pbc) % 8) - u(1, 0, zero_bit); - - *data_len = put_bits_count(&pbc); - flush_put_bits(&pbc); - + VAAPIEncodeContext *ctx = avctx->priv_data; + VAAPIEncodeMPEG2Context *priv = ctx->priv_data; + CodedBitstreamFragment *frag = &priv->current_fragment; + int err; + + err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_SEQUENCE_HEADER, + &priv->sequence_header); + if (err < 0) + goto fail; + + err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION, + &priv->sequence_extension); + if (err < 0) + goto fail; + + err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION, + &priv->sequence_display_extension); + if (err < 0) + goto fail; + + err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_GROUP, + &priv->gop_header); + if (err < 0) + goto fail; + + err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag); +fail: + ff_cbs_fragment_uninit(&priv->cbc, frag); return 0; } @@ -119,139 +140,275 @@ static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx, VAAPIEncodePicture *pic, char *data, size_t *data_len) { - VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params; - int picture_coding_type; - PutBitContext pbc; + VAAPIEncodeContext *ctx = avctx->priv_data; + VAAPIEncodeMPEG2Context *priv = ctx->priv_data; + CodedBitstreamFragment *frag = &priv->current_fragment; + int err; + + err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_PICTURE, + &priv->picture_header); + if (err < 0) + goto fail; + + err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION, + &priv->picture_coding_extension); + if (err < 0) + goto fail; + + err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag); +fail: + ff_cbs_fragment_uninit(&priv->cbc, frag); + return 0; +} - init_put_bits(&pbc, data, 8 * *data_len); +static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx) +{ + VAAPIEncodeContext *ctx = avctx->priv_data; + VAAPIEncodeMPEG2Context *priv = ctx->priv_data; + MPEG2RawSequenceHeader *sh = &priv->sequence_header; + MPEG2RawSequenceExtension *se = &priv->sequence_extension.data.sequence; + MPEG2RawSequenceDisplayExtension *sde = &priv->sequence_display_extension.data.sequence_display; + MPEG2RawGroupOfPicturesHeader *goph = &priv->gop_header; + MPEG2RawPictureHeader *ph = &priv->picture_header; + MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding; + VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params; + VAEncPictureParameterBufferMPEG2 *vpic = ctx->codec_picture_params; + int code, ext_n, ext_d; - u(32, PICTURE_START_CODE, picture_start_code); - u(10, vpic_var(temporal_reference)); + memset(sh, 0, sizeof(*sh)); + memset(se, 0, sizeof(*se)); + memset(sde, 0, sizeof(*sde)); + memset(goph, 0, sizeof(*goph)); + memset(ph, 0, sizeof(*ph)); + memset(pce, 0, sizeof(*pce)); - switch (vpic->picture_type) { - case VAEncPictureTypeIntra: - picture_coding_type = AV_PICTURE_TYPE_I; - break; - case VAEncPictureTypePredictive: - picture_coding_type = AV_PICTURE_TYPE_P; + + if (avctx->bit_rate > 0) { + priv->bit_rate = (avctx->bit_rate + 399) / 400; + } else { + // Unknown (not a bitrate-targetting mode), so just use the + // highest value. + priv->bit_rate = 0x3fffffff; + } + if (avctx->rc_buffer_size > 0) { + priv->vbv_buffer_size = (avctx->rc_buffer_size + (1 << 14) - 1) >> 14; + } else { + // Unknown, so guess a value from the bitrate. + priv->vbv_buffer_size = priv->bit_rate >> 14; + } + + switch (avctx->level) { + case 4: // High. + case 6: // High 1440. + priv->f_code_horizontal = 9; + priv->f_code_vertical = 5; break; - case VAEncPictureTypeBidirectional: - picture_coding_type = AV_PICTURE_TYPE_B; + case 8: // Main. + priv->f_code_horizontal = 8; + priv->f_code_vertical = 5; break; + case 10: // Low. default: - av_assert0(0 && "invalid picture_coding_type"); - } - u(3, picture_coding_type, picture_coding_type); - u(16, 0xffff, vbv_delay); - if (picture_coding_type == 2 || picture_coding_type == 3) { - u(1, 0, full_pel_forward_vector); - u(3, 7, forward_f_code); - } - if (picture_coding_type == 3) { - u(1, 0, full_pel_backward_vector); - u(3, 7, backward_f_code); + priv->f_code_horizontal = 7; + priv->f_code_vertical = 4; + break; } - u(1, 0, extra_bit_picture); - - while (put_bits_count(&pbc) % 8) - u(1, 0, zero_bit); - - u(32, EXT_START_CODE, extension_start_code); - u(4, 8, extension_start_code_identifier); - u(4, vpic_var(f_code[0][0])); - u(4, vpic_var(f_code[0][1])); - u(4, vpic_var(f_code[1][0])); - u(4, vpic_var(f_code[1][1])); - u(2, vpcext_field(intra_dc_precision)); - u(2, vpcext_field(picture_structure)); - u(1, vpcext_field(top_field_first)); - u(1, vpcext_field(frame_pred_frame_dct)); - u(1, vpcext_field(concealment_motion_vectors)); - u(1, vpcext_field(q_scale_type)); - u(1, vpcext_field(intra_vlc_format)); - u(1, vpcext_field(alternate_scan)); - u(1, vpcext_field(repeat_first_field)); - u(1, 1, chroma_420_type); - u(1, vpcext_field(progressive_frame)); - u(1, vpcext_field(composite_display_flag)); - if (vpic->picture_coding_extension.bits.composite_display_flag) { - u(1, vcomp_field(v_axis)); - u(3, vcomp_field(field_sequence)); - u(1, vcomp_field(sub_carrier)); - u(7, vcomp_field(burst_amplitude)); - u(8, vcomp_field(sub_carrier_phase)); + + + // Sequence header + + sh->sequence_header_code = MPEG2_START_SEQUENCE_HEADER; + + sh->horizontal_size_value = avctx->width & 0xfff; + sh->vertical_size_value = avctx->height & 0xfff; + + if (avctx->sample_aspect_ratio.num != 0 && + avctx->sample_aspect_ratio.den != 0) { + AVRational dar = av_div_q(avctx->sample_aspect_ratio, + (AVRational) { avctx->width, avctx->height }); + + if (av_cmp_q(avctx->sample_aspect_ratio, (AVRational) { 1, 1 }) == 0) { + sh->aspect_ratio_information = 1; + } else if (av_cmp_q(dar, (AVRational) { 3, 4 }) == 0) { + sh->aspect_ratio_information = 2; + } else if (av_cmp_q(dar, (AVRational) { 9, 16 }) == 0) { + sh->aspect_ratio_information = 3; + } else if (av_cmp_q(dar, (AVRational) { 100, 221 }) == 0) { + sh->aspect_ratio_information = 4; + } else { + av_log(avctx, AV_LOG_WARNING, "Sample aspect ratio %d:%d is not " + "representable, signalling square pixels instead.\n", + avctx->sample_aspect_ratio.num, + avctx->sample_aspect_ratio.den); + sh->aspect_ratio_information = 1; + } + } else { + // Unknown - assume square pixels. + sh->aspect_ratio_information = 1; } - while (put_bits_count(&pbc) % 8) - u(1, 0, zero_bit); + if (avctx->framerate.num > 0 && avctx->framerate.den > 0) + priv->frame_rate = avctx->framerate; + else + priv->frame_rate = av_inv_q(avctx->time_base); + ff_mpeg12_find_best_frame_rate(priv->frame_rate, + &code, &ext_n, &ext_d, 0); + sh->frame_rate_code = code; - *data_len = put_bits_count(&pbc); - flush_put_bits(&pbc); + sh->bit_rate_value = priv->bit_rate & 0x3ffff; + sh->vbv_buffer_size_value = priv->vbv_buffer_size & 0x3ff; - return 0; -} + sh->constrained_parameters_flag = 0; + sh->load_intra_quantiser_matrix = 0; + sh->load_non_intra_quantiser_matrix = 0; -static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx) -{ - VAAPIEncodeContext *ctx = avctx->priv_data; - VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params; - VAEncPictureParameterBufferMPEG2 *vpic = ctx->codec_picture_params; - VAAPIEncodeMPEG2Context *priv = ctx->priv_data; - vseq->intra_period = avctx->gop_size; - vseq->ip_period = ctx->b_per_p + 1; + // Sequence extension - vseq->picture_width = avctx->width; - vseq->picture_height = avctx->height; + priv->sequence_extension.extension_start_code = MPEG2_START_EXTENSION; + priv->sequence_extension.extension_start_code_identifier = + MPEG2_EXTENSION_SEQUENCE; - vseq->bits_per_second = avctx->bit_rate; - if (avctx->framerate.num > 0 && avctx->framerate.den > 0) - vseq->frame_rate = (float)avctx->framerate.num / avctx->framerate.den; - else - vseq->frame_rate = (float)avctx->time_base.num / avctx->time_base.den; - - vseq->aspect_ratio_information = 1; - vseq->vbv_buffer_size = avctx->rc_buffer_size / (16 * 1024); - - vseq->sequence_extension.bits.profile_and_level_indication = - avctx->profile << 4 | avctx->level; - vseq->sequence_extension.bits.progressive_sequence = 1; - vseq->sequence_extension.bits.chroma_format = 1; - vseq->sequence_extension.bits.low_delay = 0; - vseq->sequence_extension.bits.frame_rate_extension_n = 0; - vseq->sequence_extension.bits.frame_rate_extension_d = 0; - - vseq->new_gop_header = 0; - vseq->gop_header.bits.time_code = 0; - vseq->gop_header.bits.closed_gop = 1; - vseq->gop_header.bits.broken_link = 0; - - vpic->forward_reference_picture = VA_INVALID_ID; - vpic->backward_reference_picture = VA_INVALID_ID; - vpic->reconstructed_picture = VA_INVALID_ID; - - vpic->coded_buf = VA_INVALID_ID; - - vpic->temporal_reference = 0; - vpic->f_code[0][0] = 15; - vpic->f_code[0][1] = 15; - vpic->f_code[1][0] = 15; - vpic->f_code[1][1] = 15; - - vpic->picture_coding_extension.bits.intra_dc_precision = 0; - vpic->picture_coding_extension.bits.picture_structure = 3; - vpic->picture_coding_extension.bits.top_field_first = 0; - vpic->picture_coding_extension.bits.frame_pred_frame_dct = 1; - vpic->picture_coding_extension.bits.concealment_motion_vectors = 0; - vpic->picture_coding_extension.bits.q_scale_type = 0; - vpic->picture_coding_extension.bits.intra_vlc_format = 0; - vpic->picture_coding_extension.bits.alternate_scan = 0; - vpic->picture_coding_extension.bits.repeat_first_field = 0; - vpic->picture_coding_extension.bits.progressive_frame = 1; - vpic->picture_coding_extension.bits.composite_display_flag = 0; - - priv->bit_rate = (avctx->bit_rate + 399) / 400; - priv->vbv_buffer_size = avctx->rc_buffer_size / (16 * 1024); + se->profile_and_level_indication = avctx->profile << 4 | avctx->level; + se->progressive_sequence = 1; + se->chroma_format = 1; + + se->horizontal_size_extension = avctx->width >> 12; + se->vertical_size_extension = avctx->height >> 12; + + se->bit_rate_extension = priv->bit_rate >> 18; + se->vbv_buffer_size_extension = priv->vbv_buffer_size >> 10; + se->low_delay = ctx->b_per_p == 0; + + se->frame_rate_extension_n = ext_n; + se->frame_rate_extension_d = ext_d; + + + // Sequence display extension + + priv->sequence_display_extension.extension_start_code = + MPEG2_START_EXTENSION; + priv->sequence_display_extension.extension_start_code_identifier = + MPEG2_EXTENSION_SEQUENCE_DISPLAY; + + sde->video_format = 5; + if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || + avctx->color_trc != AVCOL_TRC_UNSPECIFIED || + avctx->colorspace != AVCOL_SPC_UNSPECIFIED) { + sde->colour_description = 1; + sde->colour_primaries = avctx->color_primaries; + sde->transfer_characteristics = avctx->color_trc; + sde->matrix_coefficients = avctx->colorspace; + } else { + sde->colour_description = 0; + } + + sde->display_horizontal_size = avctx->width; + sde->display_vertical_size = avctx->height; + + + // GOP header + + goph->group_start_code = MPEG2_START_GROUP; + + goph->time_code = 0; + goph->closed_gop = 1; + goph->broken_link = 0; + + + // Defaults for picture header + + ph->picture_start_code = MPEG2_START_PICTURE; + + ph->vbv_delay = 0xffff; // Not currently calculated. + + ph->full_pel_forward_vector = 0; + ph->forward_f_code = 7; + ph->full_pel_backward_vector = 0; + ph->forward_f_code = 7; + + + // Defaults for picture coding extension + + priv->picture_coding_extension.extension_start_code = + MPEG2_START_EXTENSION; + priv->picture_coding_extension.extension_start_code_identifier = + MPEG2_EXTENSION_PICTURE_CODING; + + pce->intra_dc_precision = 0; + pce->picture_structure = 3; + pce->top_field_first = 0; + pce->frame_pred_frame_dct = 1; + pce->concealment_motion_vectors = 0; + pce->q_scale_type = 0; + pce->intra_vlc_format = 0; + pce->alternate_scan = 0; + pce->repeat_first_field = 0; + pce->progressive_frame = 1; + pce->composite_display_flag = 0; + + + + *vseq = (VAEncSequenceParameterBufferMPEG2) { + .intra_period = avctx->gop_size, + .ip_period = ctx->b_per_p + 1, + + .picture_width = avctx->width, + .picture_height = avctx->height, + + .bits_per_second = avctx->bit_rate, + .frame_rate = av_q2d(priv->frame_rate), + .aspect_ratio_information = sh->aspect_ratio_information, + .vbv_buffer_size = priv->vbv_buffer_size, + + .sequence_extension.bits = { + .profile_and_level_indication = se->profile_and_level_indication, + .progressive_sequence = se->progressive_sequence, + .chroma_format = se->chroma_format, + .low_delay = se->low_delay, + .frame_rate_extension_n = se->frame_rate_extension_n, + .frame_rate_extension_d = se->frame_rate_extension_d, + }, + + .new_gop_header = 1, + .gop_header.bits = { + .time_code = goph->time_code, + .closed_gop = goph->closed_gop, + .broken_link = goph->broken_link, + }, + }; + + *vpic = (VAEncPictureParameterBufferMPEG2) { + .forward_reference_picture = VA_INVALID_ID, + .backward_reference_picture = VA_INVALID_ID, + .reconstructed_picture = VA_INVALID_ID, + .coded_buf = VA_INVALID_ID, + + .vbv_delay = 0xffff, + .f_code = { { 15, 15 }, { 15, 15 } }, + + .picture_coding_extension.bits = { + .intra_dc_precision = pce->intra_dc_precision, + .picture_structure = pce->picture_structure, + .top_field_first = pce->top_field_first, + .frame_pred_frame_dct = pce->frame_pred_frame_dct, + .concealment_motion_vectors = pce->concealment_motion_vectors, + .q_scale_type = pce->q_scale_type, + .intra_vlc_format = pce->intra_vlc_format, + .alternate_scan = pce->alternate_scan, + .repeat_first_field = pce->repeat_first_field, + .progressive_frame = pce->progressive_frame, + .composite_display_flag = pce->composite_display_flag, + }, + + .composite_display.bits = { + .v_axis = pce->v_axis, + .field_sequence = pce->field_sequence, + .sub_carrier = pce->sub_carrier, + .burst_amplitude = pce->burst_amplitude, + .sub_carrier_phase = pce->sub_carrier_phase, + }, + }; return 0; } @@ -260,56 +417,61 @@ static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx, VAAPIEncodePicture *pic) { VAAPIEncodeContext *ctx = avctx->priv_data; - VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params; VAAPIEncodeMPEG2Context *priv = ctx->priv_data; - int fch, fcv; + MPEG2RawPictureHeader *ph = &priv->picture_header; + MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding; + VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params; - switch (avctx->level) { - case 4: // High. - case 6: // High 1440. - fch = 9; - fcv = 5; - break; - case 8: // Main. - fch = 8; - fcv = 5; - break; - case 10: // Low. - default: - fch = 7; - fcv = 4; - break; + if (pic->type == PICTURE_TYPE_IDR || pic->type == PICTURE_TYPE_I) { + ph->temporal_reference = 0; + ph->picture_coding_type = 1; + priv->last_i_frame = pic->display_order; + } else { + ph->temporal_reference = pic->display_order - priv->last_i_frame; + ph->picture_coding_type = pic->type == PICTURE_TYPE_B ? 3 : 2; + } + + if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) { + pce->f_code[0][0] = priv->f_code_horizontal; + pce->f_code[0][1] = priv->f_code_vertical; + } else { + pce->f_code[0][0] = 15; + pce->f_code[0][1] = 15; + } + if (pic->type == PICTURE_TYPE_B) { + pce->f_code[1][0] = priv->f_code_horizontal; + pce->f_code[1][1] = priv->f_code_vertical; + } else { + pce->f_code[1][0] = 15; + pce->f_code[1][1] = 15; } + vpic->reconstructed_picture = pic->recon_surface; + vpic->coded_buf = pic->output_buffer; + switch (pic->type) { case PICTURE_TYPE_IDR: case PICTURE_TYPE_I: vpic->picture_type = VAEncPictureTypeIntra; - priv->last_i_frame = pic->display_order; break; case PICTURE_TYPE_P: vpic->picture_type = VAEncPictureTypePredictive; vpic->forward_reference_picture = pic->refs[0]->recon_surface; - vpic->f_code[0][0] = fch; - vpic->f_code[0][1] = fcv; break; case PICTURE_TYPE_B: vpic->picture_type = VAEncPictureTypeBidirectional; vpic->forward_reference_picture = pic->refs[0]->recon_surface; vpic->backward_reference_picture = pic->refs[1]->recon_surface; - vpic->f_code[0][0] = fch; - vpic->f_code[0][1] = fcv; - vpic->f_code[1][0] = fch; - vpic->f_code[1][1] = fcv; break; default: av_assert0(0 && "invalid picture type"); } - vpic->reconstructed_picture = pic->recon_surface; - vpic->coded_buf = pic->output_buffer; - - vpic->temporal_reference = pic->display_order - priv->last_i_frame; + vpic->temporal_reference = ph->temporal_reference; + vpic->f_code[0][0] = pce->f_code[0][0]; + vpic->f_code[0][1] = pce->f_code[0][1]; + vpic->f_code[1][0] = pce->f_code[1][0]; + vpic->f_code[1][1] = pce->f_code[1][1]; pic->nb_slices = priv->mb_height; @@ -354,6 +516,11 @@ static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx) { VAAPIEncodeContext *ctx = avctx->priv_data; VAAPIEncodeMPEG2Context *priv = ctx->priv_data; + int err; + + err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx); + if (err < 0) + return err; priv->mb_width = FFALIGN(avctx->width, 16) / 16; priv->mb_height = FFALIGN(avctx->height, 16) / 16; @@ -420,11 +587,41 @@ static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx) case FF_PROFILE_MPEG2_MAIN: ctx->va_profile = VAProfileMPEG2Main; break; + case FF_PROFILE_MPEG2_422: + av_log(avctx, AV_LOG_ERROR, "MPEG-2 4:2:2 profile " + "is not supported.\n"); + return AVERROR_PATCHWELCOME; + case FF_PROFILE_MPEG2_HIGH: + av_log(avctx, AV_LOG_ERROR, "MPEG-2 high profile " + "is not supported.\n"); + return AVERROR_PATCHWELCOME; + case FF_PROFILE_MPEG2_SS: + case FF_PROFILE_MPEG2_SNR_SCALABLE: + av_log(avctx, AV_LOG_ERROR, "MPEG-2 scalable profiles " + "are not supported.\n"); + return AVERROR_PATCHWELCOME; default: av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 profile %d.\n", avctx->profile); return AVERROR(EINVAL); } + switch (avctx->level) { + case 4: // High + case 6: // High 1440 + case 8: // Main + case 10: // Low + break; + default: + av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 level %d.\n", + avctx->level); + return AVERROR(EINVAL); + } + + if (avctx->height % 4096 == 0 || avctx->width % 4096 == 0) { + av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support picture " + "height or width divisible by 4096.\n"); + return AVERROR(EINVAL); + } ctx->va_entrypoint = VAEntrypointEncSlice; ctx->va_rt_format = VA_RT_FORMAT_YUV420; @@ -439,6 +636,17 @@ static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx) return ff_vaapi_encode_init(avctx); } +static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx) +{ + VAAPIEncodeContext *ctx = avctx->priv_data; + VAAPIEncodeMPEG2Context *priv = ctx->priv_data; + + if (priv) + ff_cbs_close(&priv->cbc); + + return ff_vaapi_encode_close(avctx); +} + static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = { { "profile", "4" }, { "level", "4" }, @@ -460,7 +668,7 @@ AVCodec ff_mpeg2_vaapi_encoder = { .priv_data_size = sizeof(VAAPIEncodeContext), .init = &vaapi_encode_mpeg2_init, .encode2 = &ff_vaapi_encode2, - .close = &ff_vaapi_encode_close, + .close = &vaapi_encode_mpeg2_close, .capabilities = AV_CODEC_CAP_DELAY, .defaults = vaapi_encode_mpeg2_defaults, .pix_fmts = (const enum AVPixelFormat[]) {