You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

680 lines
23KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <va/va.h>
  19. #include <va/va_enc_mpeg2.h>
  20. #include "libavutil/avassert.h"
  21. #include "avcodec.h"
  22. #include "cbs.h"
  23. #include "cbs_mpeg2.h"
  24. #include "mpeg12.h"
  25. #include "vaapi_encode.h"
  26. typedef struct VAAPIEncodeMPEG2Context {
  27. int mb_width;
  28. int mb_height;
  29. int quant_i;
  30. int quant_p;
  31. int quant_b;
  32. MPEG2RawSequenceHeader sequence_header;
  33. MPEG2RawExtensionData sequence_extension;
  34. MPEG2RawExtensionData sequence_display_extension;
  35. MPEG2RawGroupOfPicturesHeader gop_header;
  36. MPEG2RawPictureHeader picture_header;
  37. MPEG2RawExtensionData picture_coding_extension;
  38. int64_t last_i_frame;
  39. unsigned int bit_rate;
  40. unsigned int vbv_buffer_size;
  41. AVRational frame_rate;
  42. unsigned int f_code_horizontal;
  43. unsigned int f_code_vertical;
  44. CodedBitstreamContext *cbc;
  45. CodedBitstreamFragment current_fragment;
  46. } VAAPIEncodeMPEG2Context;
  47. static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx,
  48. char *data, size_t *data_len,
  49. CodedBitstreamFragment *frag)
  50. {
  51. VAAPIEncodeContext *ctx = avctx->priv_data;
  52. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  53. int err;
  54. err = ff_cbs_write_fragment_data(priv->cbc, frag);
  55. if (err < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
  57. return err;
  58. }
  59. if (*data_len < 8 * frag->data_size - frag->data_bit_padding) {
  60. av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
  61. "%zu < %zu.\n", *data_len,
  62. 8 * frag->data_size - frag->data_bit_padding);
  63. return AVERROR(ENOSPC);
  64. }
  65. memcpy(data, frag->data, frag->data_size);
  66. *data_len = 8 * frag->data_size - frag->data_bit_padding;
  67. return 0;
  68. }
  69. static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx,
  70. CodedBitstreamFragment *frag,
  71. int type, void *header)
  72. {
  73. VAAPIEncodeContext *ctx = avctx->priv_data;
  74. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  75. int err;
  76. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1, type, header);
  77. if (err < 0) {
  78. av_log(avctx, AV_LOG_ERROR, "Failed to add header: "
  79. "type = %d.\n", type);
  80. return err;
  81. }
  82. return 0;
  83. }
  84. static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx,
  85. char *data, size_t *data_len)
  86. {
  87. VAAPIEncodeContext *ctx = avctx->priv_data;
  88. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  89. CodedBitstreamFragment *frag = &priv->current_fragment;
  90. int err;
  91. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_SEQUENCE_HEADER,
  92. &priv->sequence_header);
  93. if (err < 0)
  94. goto fail;
  95. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  96. &priv->sequence_extension);
  97. if (err < 0)
  98. goto fail;
  99. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  100. &priv->sequence_display_extension);
  101. if (err < 0)
  102. goto fail;
  103. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_GROUP,
  104. &priv->gop_header);
  105. if (err < 0)
  106. goto fail;
  107. err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
  108. fail:
  109. ff_cbs_fragment_uninit(priv->cbc, frag);
  110. return 0;
  111. }
  112. static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx,
  113. VAAPIEncodePicture *pic,
  114. char *data, size_t *data_len)
  115. {
  116. VAAPIEncodeContext *ctx = avctx->priv_data;
  117. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  118. CodedBitstreamFragment *frag = &priv->current_fragment;
  119. int err;
  120. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_PICTURE,
  121. &priv->picture_header);
  122. if (err < 0)
  123. goto fail;
  124. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  125. &priv->picture_coding_extension);
  126. if (err < 0)
  127. goto fail;
  128. err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
  129. fail:
  130. ff_cbs_fragment_uninit(priv->cbc, frag);
  131. return 0;
  132. }
  133. static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
  134. {
  135. VAAPIEncodeContext *ctx = avctx->priv_data;
  136. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  137. MPEG2RawSequenceHeader *sh = &priv->sequence_header;
  138. MPEG2RawSequenceExtension *se = &priv->sequence_extension.data.sequence;
  139. MPEG2RawSequenceDisplayExtension *sde = &priv->sequence_display_extension.data.sequence_display;
  140. MPEG2RawGroupOfPicturesHeader *goph = &priv->gop_header;
  141. MPEG2RawPictureHeader *ph = &priv->picture_header;
  142. MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
  143. VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params;
  144. VAEncPictureParameterBufferMPEG2 *vpic = ctx->codec_picture_params;
  145. int code, ext_n, ext_d;
  146. memset(sh, 0, sizeof(*sh));
  147. memset(se, 0, sizeof(*se));
  148. memset(sde, 0, sizeof(*sde));
  149. memset(goph, 0, sizeof(*goph));
  150. memset(ph, 0, sizeof(*ph));
  151. memset(pce, 0, sizeof(*pce));
  152. if (avctx->bit_rate > 0) {
  153. priv->bit_rate = (avctx->bit_rate + 399) / 400;
  154. } else {
  155. // Unknown (not a bitrate-targetting mode), so just use the
  156. // highest value.
  157. priv->bit_rate = 0x3fffffff;
  158. }
  159. if (avctx->rc_buffer_size > 0) {
  160. priv->vbv_buffer_size = (avctx->rc_buffer_size + (1 << 14) - 1) >> 14;
  161. } else {
  162. // Unknown, so guess a value from the bitrate.
  163. priv->vbv_buffer_size = priv->bit_rate >> 14;
  164. }
  165. switch (avctx->level) {
  166. case 4: // High.
  167. case 6: // High 1440.
  168. priv->f_code_horizontal = 9;
  169. priv->f_code_vertical = 5;
  170. break;
  171. case 8: // Main.
  172. priv->f_code_horizontal = 8;
  173. priv->f_code_vertical = 5;
  174. break;
  175. case 10: // Low.
  176. default:
  177. priv->f_code_horizontal = 7;
  178. priv->f_code_vertical = 4;
  179. break;
  180. }
  181. // Sequence header
  182. sh->sequence_header_code = MPEG2_START_SEQUENCE_HEADER;
  183. sh->horizontal_size_value = avctx->width & 0xfff;
  184. sh->vertical_size_value = avctx->height & 0xfff;
  185. if (avctx->sample_aspect_ratio.num != 0 &&
  186. avctx->sample_aspect_ratio.den != 0) {
  187. AVRational dar = av_div_q(avctx->sample_aspect_ratio,
  188. (AVRational) { avctx->width, avctx->height });
  189. if (av_cmp_q(avctx->sample_aspect_ratio, (AVRational) { 1, 1 }) == 0) {
  190. sh->aspect_ratio_information = 1;
  191. } else if (av_cmp_q(dar, (AVRational) { 3, 4 }) == 0) {
  192. sh->aspect_ratio_information = 2;
  193. } else if (av_cmp_q(dar, (AVRational) { 9, 16 }) == 0) {
  194. sh->aspect_ratio_information = 3;
  195. } else if (av_cmp_q(dar, (AVRational) { 100, 221 }) == 0) {
  196. sh->aspect_ratio_information = 4;
  197. } else {
  198. av_log(avctx, AV_LOG_WARNING, "Sample aspect ratio %d:%d is not "
  199. "representable, signalling square pixels instead.\n",
  200. avctx->sample_aspect_ratio.num,
  201. avctx->sample_aspect_ratio.den);
  202. sh->aspect_ratio_information = 1;
  203. }
  204. } else {
  205. // Unknown - assume square pixels.
  206. sh->aspect_ratio_information = 1;
  207. }
  208. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  209. priv->frame_rate = avctx->framerate;
  210. else
  211. priv->frame_rate = av_inv_q(avctx->time_base);
  212. ff_mpeg12_find_best_frame_rate(priv->frame_rate,
  213. &code, &ext_n, &ext_d, 0);
  214. sh->frame_rate_code = code;
  215. sh->bit_rate_value = priv->bit_rate & 0x3ffff;
  216. sh->vbv_buffer_size_value = priv->vbv_buffer_size & 0x3ff;
  217. sh->constrained_parameters_flag = 0;
  218. sh->load_intra_quantiser_matrix = 0;
  219. sh->load_non_intra_quantiser_matrix = 0;
  220. // Sequence extension
  221. priv->sequence_extension.extension_start_code = MPEG2_START_EXTENSION;
  222. priv->sequence_extension.extension_start_code_identifier =
  223. MPEG2_EXTENSION_SEQUENCE;
  224. se->profile_and_level_indication = avctx->profile << 4 | avctx->level;
  225. se->progressive_sequence = 1;
  226. se->chroma_format = 1;
  227. se->horizontal_size_extension = avctx->width >> 12;
  228. se->vertical_size_extension = avctx->height >> 12;
  229. se->bit_rate_extension = priv->bit_rate >> 18;
  230. se->vbv_buffer_size_extension = priv->vbv_buffer_size >> 10;
  231. se->low_delay = ctx->b_per_p == 0;
  232. se->frame_rate_extension_n = ext_n;
  233. se->frame_rate_extension_d = ext_d;
  234. // Sequence display extension
  235. priv->sequence_display_extension.extension_start_code =
  236. MPEG2_START_EXTENSION;
  237. priv->sequence_display_extension.extension_start_code_identifier =
  238. MPEG2_EXTENSION_SEQUENCE_DISPLAY;
  239. sde->video_format = 5;
  240. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  241. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  242. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  243. sde->colour_description = 1;
  244. sde->colour_primaries = avctx->color_primaries;
  245. sde->transfer_characteristics = avctx->color_trc;
  246. sde->matrix_coefficients = avctx->colorspace;
  247. } else {
  248. sde->colour_description = 0;
  249. }
  250. sde->display_horizontal_size = avctx->width;
  251. sde->display_vertical_size = avctx->height;
  252. // GOP header
  253. goph->group_start_code = MPEG2_START_GROUP;
  254. goph->time_code = 0;
  255. goph->closed_gop = 1;
  256. goph->broken_link = 0;
  257. // Defaults for picture header
  258. ph->picture_start_code = MPEG2_START_PICTURE;
  259. ph->vbv_delay = 0xffff; // Not currently calculated.
  260. ph->full_pel_forward_vector = 0;
  261. ph->forward_f_code = 7;
  262. ph->full_pel_backward_vector = 0;
  263. ph->forward_f_code = 7;
  264. // Defaults for picture coding extension
  265. priv->picture_coding_extension.extension_start_code =
  266. MPEG2_START_EXTENSION;
  267. priv->picture_coding_extension.extension_start_code_identifier =
  268. MPEG2_EXTENSION_PICTURE_CODING;
  269. pce->intra_dc_precision = 0;
  270. pce->picture_structure = 3;
  271. pce->top_field_first = 0;
  272. pce->frame_pred_frame_dct = 1;
  273. pce->concealment_motion_vectors = 0;
  274. pce->q_scale_type = 0;
  275. pce->intra_vlc_format = 0;
  276. pce->alternate_scan = 0;
  277. pce->repeat_first_field = 0;
  278. pce->progressive_frame = 1;
  279. pce->composite_display_flag = 0;
  280. *vseq = (VAEncSequenceParameterBufferMPEG2) {
  281. .intra_period = avctx->gop_size,
  282. .ip_period = ctx->b_per_p + 1,
  283. .picture_width = avctx->width,
  284. .picture_height = avctx->height,
  285. .bits_per_second = avctx->bit_rate,
  286. .frame_rate = av_q2d(priv->frame_rate),
  287. .aspect_ratio_information = sh->aspect_ratio_information,
  288. .vbv_buffer_size = priv->vbv_buffer_size,
  289. .sequence_extension.bits = {
  290. .profile_and_level_indication = se->profile_and_level_indication,
  291. .progressive_sequence = se->progressive_sequence,
  292. .chroma_format = se->chroma_format,
  293. .low_delay = se->low_delay,
  294. .frame_rate_extension_n = se->frame_rate_extension_n,
  295. .frame_rate_extension_d = se->frame_rate_extension_d,
  296. },
  297. .new_gop_header = 1,
  298. .gop_header.bits = {
  299. .time_code = goph->time_code,
  300. .closed_gop = goph->closed_gop,
  301. .broken_link = goph->broken_link,
  302. },
  303. };
  304. *vpic = (VAEncPictureParameterBufferMPEG2) {
  305. .forward_reference_picture = VA_INVALID_ID,
  306. .backward_reference_picture = VA_INVALID_ID,
  307. .reconstructed_picture = VA_INVALID_ID,
  308. .coded_buf = VA_INVALID_ID,
  309. .vbv_delay = 0xffff,
  310. .f_code = { { 15, 15 }, { 15, 15 } },
  311. .picture_coding_extension.bits = {
  312. .intra_dc_precision = pce->intra_dc_precision,
  313. .picture_structure = pce->picture_structure,
  314. .top_field_first = pce->top_field_first,
  315. .frame_pred_frame_dct = pce->frame_pred_frame_dct,
  316. .concealment_motion_vectors = pce->concealment_motion_vectors,
  317. .q_scale_type = pce->q_scale_type,
  318. .intra_vlc_format = pce->intra_vlc_format,
  319. .alternate_scan = pce->alternate_scan,
  320. .repeat_first_field = pce->repeat_first_field,
  321. .progressive_frame = pce->progressive_frame,
  322. .composite_display_flag = pce->composite_display_flag,
  323. },
  324. .composite_display.bits = {
  325. .v_axis = pce->v_axis,
  326. .field_sequence = pce->field_sequence,
  327. .sub_carrier = pce->sub_carrier,
  328. .burst_amplitude = pce->burst_amplitude,
  329. .sub_carrier_phase = pce->sub_carrier_phase,
  330. },
  331. };
  332. return 0;
  333. }
  334. static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
  335. VAAPIEncodePicture *pic)
  336. {
  337. VAAPIEncodeContext *ctx = avctx->priv_data;
  338. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  339. MPEG2RawPictureHeader *ph = &priv->picture_header;
  340. MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
  341. VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params;
  342. if (pic->type == PICTURE_TYPE_IDR || pic->type == PICTURE_TYPE_I) {
  343. ph->temporal_reference = 0;
  344. ph->picture_coding_type = 1;
  345. priv->last_i_frame = pic->display_order;
  346. } else {
  347. ph->temporal_reference = pic->display_order - priv->last_i_frame;
  348. ph->picture_coding_type = pic->type == PICTURE_TYPE_B ? 3 : 2;
  349. }
  350. if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
  351. pce->f_code[0][0] = priv->f_code_horizontal;
  352. pce->f_code[0][1] = priv->f_code_vertical;
  353. } else {
  354. pce->f_code[0][0] = 15;
  355. pce->f_code[0][1] = 15;
  356. }
  357. if (pic->type == PICTURE_TYPE_B) {
  358. pce->f_code[1][0] = priv->f_code_horizontal;
  359. pce->f_code[1][1] = priv->f_code_vertical;
  360. } else {
  361. pce->f_code[1][0] = 15;
  362. pce->f_code[1][1] = 15;
  363. }
  364. vpic->reconstructed_picture = pic->recon_surface;
  365. vpic->coded_buf = pic->output_buffer;
  366. switch (pic->type) {
  367. case PICTURE_TYPE_IDR:
  368. case PICTURE_TYPE_I:
  369. vpic->picture_type = VAEncPictureTypeIntra;
  370. break;
  371. case PICTURE_TYPE_P:
  372. vpic->picture_type = VAEncPictureTypePredictive;
  373. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  374. break;
  375. case PICTURE_TYPE_B:
  376. vpic->picture_type = VAEncPictureTypeBidirectional;
  377. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  378. vpic->backward_reference_picture = pic->refs[1]->recon_surface;
  379. break;
  380. default:
  381. av_assert0(0 && "invalid picture type");
  382. }
  383. vpic->temporal_reference = ph->temporal_reference;
  384. vpic->f_code[0][0] = pce->f_code[0][0];
  385. vpic->f_code[0][1] = pce->f_code[0][1];
  386. vpic->f_code[1][0] = pce->f_code[1][0];
  387. vpic->f_code[1][1] = pce->f_code[1][1];
  388. pic->nb_slices = priv->mb_height;
  389. return 0;
  390. }
  391. static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
  392. VAAPIEncodePicture *pic,
  393. VAAPIEncodeSlice *slice)
  394. {
  395. VAAPIEncodeContext *ctx = avctx->priv_data;
  396. VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
  397. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  398. int qp;
  399. vslice->macroblock_address = priv->mb_width * slice->index;
  400. vslice->num_macroblocks = priv->mb_width;
  401. switch (pic->type) {
  402. case PICTURE_TYPE_IDR:
  403. case PICTURE_TYPE_I:
  404. qp = priv->quant_i;
  405. break;
  406. case PICTURE_TYPE_P:
  407. qp = priv->quant_p;
  408. break;
  409. case PICTURE_TYPE_B:
  410. qp = priv->quant_b;
  411. break;
  412. default:
  413. av_assert0(0 && "invalid picture type");
  414. }
  415. vslice->quantiser_scale_code = qp;
  416. vslice->is_intra_slice = (pic->type == PICTURE_TYPE_IDR ||
  417. pic->type == PICTURE_TYPE_I);
  418. return 0;
  419. }
  420. static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
  421. {
  422. VAAPIEncodeContext *ctx = avctx->priv_data;
  423. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  424. int err;
  425. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
  426. if (err < 0)
  427. return err;
  428. priv->mb_width = FFALIGN(avctx->width, 16) / 16;
  429. priv->mb_height = FFALIGN(avctx->height, 16) / 16;
  430. if (ctx->va_rc_mode == VA_RC_CQP) {
  431. priv->quant_p = av_clip(avctx->global_quality, 1, 31);
  432. if (avctx->i_quant_factor > 0.0)
  433. priv->quant_i = av_clip((avctx->global_quality *
  434. avctx->i_quant_factor +
  435. avctx->i_quant_offset) + 0.5,
  436. 1, 31);
  437. else
  438. priv->quant_i = priv->quant_p;
  439. if (avctx->b_quant_factor > 0.0)
  440. priv->quant_b = av_clip((avctx->global_quality *
  441. avctx->b_quant_factor +
  442. avctx->b_quant_offset) + 0.5,
  443. 1, 31);
  444. else
  445. priv->quant_b = priv->quant_p;
  446. av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
  447. "%d / %d / %d for I- / P- / B-frames.\n",
  448. priv->quant_i, priv->quant_p, priv->quant_b);
  449. } else {
  450. av_assert0(0 && "Invalid RC mode.");
  451. }
  452. return 0;
  453. }
  454. static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
  455. .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
  456. .configure = &vaapi_encode_mpeg2_configure,
  457. .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
  458. .init_sequence_params = &vaapi_encode_mpeg2_init_sequence_params,
  459. .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
  460. .init_picture_params = &vaapi_encode_mpeg2_init_picture_params,
  461. .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
  462. .init_slice_params = &vaapi_encode_mpeg2_init_slice_params,
  463. .sequence_header_type = VAEncPackedHeaderSequence,
  464. .write_sequence_header = &vaapi_encode_mpeg2_write_sequence_header,
  465. .picture_header_type = VAEncPackedHeaderPicture,
  466. .write_picture_header = &vaapi_encode_mpeg2_write_picture_header,
  467. };
  468. static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
  469. {
  470. VAAPIEncodeContext *ctx = avctx->priv_data;
  471. ctx->codec = &vaapi_encode_type_mpeg2;
  472. switch (avctx->profile) {
  473. case FF_PROFILE_MPEG2_SIMPLE:
  474. ctx->va_profile = VAProfileMPEG2Simple;
  475. break;
  476. case FF_PROFILE_MPEG2_MAIN:
  477. ctx->va_profile = VAProfileMPEG2Main;
  478. break;
  479. case FF_PROFILE_MPEG2_422:
  480. av_log(avctx, AV_LOG_ERROR, "MPEG-2 4:2:2 profile "
  481. "is not supported.\n");
  482. return AVERROR_PATCHWELCOME;
  483. case FF_PROFILE_MPEG2_HIGH:
  484. av_log(avctx, AV_LOG_ERROR, "MPEG-2 high profile "
  485. "is not supported.\n");
  486. return AVERROR_PATCHWELCOME;
  487. case FF_PROFILE_MPEG2_SS:
  488. case FF_PROFILE_MPEG2_SNR_SCALABLE:
  489. av_log(avctx, AV_LOG_ERROR, "MPEG-2 scalable profiles "
  490. "are not supported.\n");
  491. return AVERROR_PATCHWELCOME;
  492. default:
  493. av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 profile %d.\n",
  494. avctx->profile);
  495. return AVERROR(EINVAL);
  496. }
  497. switch (avctx->level) {
  498. case 4: // High
  499. case 6: // High 1440
  500. case 8: // Main
  501. case 10: // Low
  502. break;
  503. default:
  504. av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 level %d.\n",
  505. avctx->level);
  506. return AVERROR(EINVAL);
  507. }
  508. if (avctx->height % 4096 == 0 || avctx->width % 4096 == 0) {
  509. av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support picture "
  510. "height or width divisible by 4096.\n");
  511. return AVERROR(EINVAL);
  512. }
  513. ctx->va_entrypoint = VAEntrypointEncSlice;
  514. ctx->va_rt_format = VA_RT_FORMAT_YUV420;
  515. ctx->va_rc_mode = VA_RC_CQP;
  516. ctx->va_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
  517. VA_ENC_PACKED_HEADER_PICTURE;
  518. ctx->surface_width = FFALIGN(avctx->width, 16);
  519. ctx->surface_height = FFALIGN(avctx->height, 16);
  520. return ff_vaapi_encode_init(avctx);
  521. }
  522. static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
  523. {
  524. VAAPIEncodeContext *ctx = avctx->priv_data;
  525. VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
  526. if (priv)
  527. ff_cbs_close(&priv->cbc);
  528. return ff_vaapi_encode_close(avctx);
  529. }
  530. static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = {
  531. { "profile", "4" },
  532. { "level", "4" },
  533. { "bf", "1" },
  534. { "g", "120" },
  535. { "i_qfactor", "1" },
  536. { "i_qoffset", "0" },
  537. { "b_qfactor", "6/5" },
  538. { "b_qoffset", "0" },
  539. { "global_quality", "10" },
  540. { NULL },
  541. };
  542. AVCodec ff_mpeg2_vaapi_encoder = {
  543. .name = "mpeg2_vaapi",
  544. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 (VAAPI)"),
  545. .type = AVMEDIA_TYPE_VIDEO,
  546. .id = AV_CODEC_ID_MPEG2VIDEO,
  547. .priv_data_size = sizeof(VAAPIEncodeContext),
  548. .init = &vaapi_encode_mpeg2_init,
  549. .encode2 = &ff_vaapi_encode2,
  550. .close = &vaapi_encode_mpeg2_close,
  551. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  552. .defaults = vaapi_encode_mpeg2_defaults,
  553. .pix_fmts = (const enum AVPixelFormat[]) {
  554. AV_PIX_FMT_VAAPI,
  555. AV_PIX_FMT_NONE,
  556. },
  557. .wrapper_name = "vaapi",
  558. };