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.

677 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. VAAPIEncodeContext common;
  28. // Derived settings.
  29. int mb_width;
  30. int mb_height;
  31. int quant_i;
  32. int quant_p;
  33. int quant_b;
  34. unsigned int bit_rate;
  35. unsigned int vbv_buffer_size;
  36. AVRational frame_rate;
  37. unsigned int f_code_horizontal;
  38. unsigned int f_code_vertical;
  39. // Stream state.
  40. int64_t last_i_frame;
  41. // Writer structures.
  42. MPEG2RawSequenceHeader sequence_header;
  43. MPEG2RawExtensionData sequence_extension;
  44. MPEG2RawExtensionData sequence_display_extension;
  45. MPEG2RawGroupOfPicturesHeader gop_header;
  46. MPEG2RawPictureHeader picture_header;
  47. MPEG2RawExtensionData picture_coding_extension;
  48. CodedBitstreamContext *cbc;
  49. CodedBitstreamFragment current_fragment;
  50. } VAAPIEncodeMPEG2Context;
  51. static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx,
  52. char *data, size_t *data_len,
  53. CodedBitstreamFragment *frag)
  54. {
  55. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  56. int err;
  57. err = ff_cbs_write_fragment_data(priv->cbc, frag);
  58. if (err < 0) {
  59. av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
  60. return err;
  61. }
  62. if (*data_len < 8 * frag->data_size - frag->data_bit_padding) {
  63. av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
  64. "%zu < %zu.\n", *data_len,
  65. 8 * frag->data_size - frag->data_bit_padding);
  66. return AVERROR(ENOSPC);
  67. }
  68. memcpy(data, frag->data, frag->data_size);
  69. *data_len = 8 * frag->data_size - frag->data_bit_padding;
  70. return 0;
  71. }
  72. static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx,
  73. CodedBitstreamFragment *frag,
  74. int type, void *header)
  75. {
  76. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  77. int err;
  78. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1, type, header, NULL);
  79. if (err < 0) {
  80. av_log(avctx, AV_LOG_ERROR, "Failed to add header: "
  81. "type = %d.\n", type);
  82. return err;
  83. }
  84. return 0;
  85. }
  86. static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx,
  87. char *data, size_t *data_len)
  88. {
  89. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  90. CodedBitstreamFragment *frag = &priv->current_fragment;
  91. int err;
  92. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_SEQUENCE_HEADER,
  93. &priv->sequence_header);
  94. if (err < 0)
  95. goto fail;
  96. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  97. &priv->sequence_extension);
  98. if (err < 0)
  99. goto fail;
  100. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  101. &priv->sequence_display_extension);
  102. if (err < 0)
  103. goto fail;
  104. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_GROUP,
  105. &priv->gop_header);
  106. if (err < 0)
  107. goto fail;
  108. err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
  109. fail:
  110. ff_cbs_fragment_uninit(priv->cbc, frag);
  111. return 0;
  112. }
  113. static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx,
  114. VAAPIEncodePicture *pic,
  115. char *data, size_t *data_len)
  116. {
  117. VAAPIEncodeMPEG2Context *priv = avctx->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 = avctx->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. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  338. MPEG2RawPictureHeader *ph = &priv->picture_header;
  339. MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
  340. VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params;
  341. if (pic->type == PICTURE_TYPE_IDR || pic->type == PICTURE_TYPE_I) {
  342. ph->temporal_reference = 0;
  343. ph->picture_coding_type = 1;
  344. priv->last_i_frame = pic->display_order;
  345. } else {
  346. ph->temporal_reference = pic->display_order - priv->last_i_frame;
  347. ph->picture_coding_type = pic->type == PICTURE_TYPE_B ? 3 : 2;
  348. }
  349. if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
  350. pce->f_code[0][0] = priv->f_code_horizontal;
  351. pce->f_code[0][1] = priv->f_code_vertical;
  352. } else {
  353. pce->f_code[0][0] = 15;
  354. pce->f_code[0][1] = 15;
  355. }
  356. if (pic->type == PICTURE_TYPE_B) {
  357. pce->f_code[1][0] = priv->f_code_horizontal;
  358. pce->f_code[1][1] = priv->f_code_vertical;
  359. } else {
  360. pce->f_code[1][0] = 15;
  361. pce->f_code[1][1] = 15;
  362. }
  363. vpic->reconstructed_picture = pic->recon_surface;
  364. vpic->coded_buf = pic->output_buffer;
  365. switch (pic->type) {
  366. case PICTURE_TYPE_IDR:
  367. case PICTURE_TYPE_I:
  368. vpic->picture_type = VAEncPictureTypeIntra;
  369. break;
  370. case PICTURE_TYPE_P:
  371. vpic->picture_type = VAEncPictureTypePredictive;
  372. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  373. break;
  374. case PICTURE_TYPE_B:
  375. vpic->picture_type = VAEncPictureTypeBidirectional;
  376. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  377. vpic->backward_reference_picture = pic->refs[1]->recon_surface;
  378. break;
  379. default:
  380. av_assert0(0 && "invalid picture type");
  381. }
  382. vpic->temporal_reference = ph->temporal_reference;
  383. vpic->f_code[0][0] = pce->f_code[0][0];
  384. vpic->f_code[0][1] = pce->f_code[0][1];
  385. vpic->f_code[1][0] = pce->f_code[1][0];
  386. vpic->f_code[1][1] = pce->f_code[1][1];
  387. pic->nb_slices = priv->mb_height;
  388. return 0;
  389. }
  390. static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
  391. VAAPIEncodePicture *pic,
  392. VAAPIEncodeSlice *slice)
  393. {
  394. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  395. VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
  396. int qp;
  397. vslice->macroblock_address = priv->mb_width * slice->index;
  398. vslice->num_macroblocks = priv->mb_width;
  399. switch (pic->type) {
  400. case PICTURE_TYPE_IDR:
  401. case PICTURE_TYPE_I:
  402. qp = priv->quant_i;
  403. break;
  404. case PICTURE_TYPE_P:
  405. qp = priv->quant_p;
  406. break;
  407. case PICTURE_TYPE_B:
  408. qp = priv->quant_b;
  409. break;
  410. default:
  411. av_assert0(0 && "invalid picture type");
  412. }
  413. vslice->quantiser_scale_code = qp;
  414. vslice->is_intra_slice = (pic->type == PICTURE_TYPE_IDR ||
  415. pic->type == PICTURE_TYPE_I);
  416. return 0;
  417. }
  418. static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
  419. {
  420. VAAPIEncodeContext *ctx = avctx->priv_data;
  421. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  422. int err;
  423. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
  424. if (err < 0)
  425. return err;
  426. priv->mb_width = FFALIGN(avctx->width, 16) / 16;
  427. priv->mb_height = FFALIGN(avctx->height, 16) / 16;
  428. if (ctx->va_rc_mode == VA_RC_CQP) {
  429. priv->quant_p = av_clip(avctx->global_quality, 1, 31);
  430. if (avctx->i_quant_factor > 0.0)
  431. priv->quant_i = av_clip((avctx->global_quality *
  432. avctx->i_quant_factor +
  433. avctx->i_quant_offset) + 0.5,
  434. 1, 31);
  435. else
  436. priv->quant_i = priv->quant_p;
  437. if (avctx->b_quant_factor > 0.0)
  438. priv->quant_b = av_clip((avctx->global_quality *
  439. avctx->b_quant_factor +
  440. avctx->b_quant_offset) + 0.5,
  441. 1, 31);
  442. else
  443. priv->quant_b = priv->quant_p;
  444. av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
  445. "%d / %d / %d for I- / P- / B-frames.\n",
  446. priv->quant_i, priv->quant_p, priv->quant_b);
  447. } else {
  448. av_assert0(0 && "Invalid RC mode.");
  449. }
  450. return 0;
  451. }
  452. static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
  453. .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
  454. .configure = &vaapi_encode_mpeg2_configure,
  455. .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
  456. .init_sequence_params = &vaapi_encode_mpeg2_init_sequence_params,
  457. .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
  458. .init_picture_params = &vaapi_encode_mpeg2_init_picture_params,
  459. .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
  460. .init_slice_params = &vaapi_encode_mpeg2_init_slice_params,
  461. .sequence_header_type = VAEncPackedHeaderSequence,
  462. .write_sequence_header = &vaapi_encode_mpeg2_write_sequence_header,
  463. .picture_header_type = VAEncPackedHeaderPicture,
  464. .write_picture_header = &vaapi_encode_mpeg2_write_picture_header,
  465. };
  466. static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
  467. {
  468. VAAPIEncodeContext *ctx = avctx->priv_data;
  469. ctx->codec = &vaapi_encode_type_mpeg2;
  470. switch (avctx->profile) {
  471. case FF_PROFILE_MPEG2_SIMPLE:
  472. ctx->va_profile = VAProfileMPEG2Simple;
  473. break;
  474. case FF_PROFILE_MPEG2_MAIN:
  475. ctx->va_profile = VAProfileMPEG2Main;
  476. break;
  477. case FF_PROFILE_MPEG2_422:
  478. av_log(avctx, AV_LOG_ERROR, "MPEG-2 4:2:2 profile "
  479. "is not supported.\n");
  480. return AVERROR_PATCHWELCOME;
  481. case FF_PROFILE_MPEG2_HIGH:
  482. av_log(avctx, AV_LOG_ERROR, "MPEG-2 high profile "
  483. "is not supported.\n");
  484. return AVERROR_PATCHWELCOME;
  485. case FF_PROFILE_MPEG2_SS:
  486. case FF_PROFILE_MPEG2_SNR_SCALABLE:
  487. av_log(avctx, AV_LOG_ERROR, "MPEG-2 scalable profiles "
  488. "are not supported.\n");
  489. return AVERROR_PATCHWELCOME;
  490. default:
  491. av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 profile %d.\n",
  492. avctx->profile);
  493. return AVERROR(EINVAL);
  494. }
  495. switch (avctx->level) {
  496. case 4: // High
  497. case 6: // High 1440
  498. case 8: // Main
  499. case 10: // Low
  500. break;
  501. default:
  502. av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 level %d.\n",
  503. avctx->level);
  504. return AVERROR(EINVAL);
  505. }
  506. if (avctx->height % 4096 == 0 || avctx->width % 4096 == 0) {
  507. av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support picture "
  508. "height or width divisible by 4096.\n");
  509. return AVERROR(EINVAL);
  510. }
  511. ctx->va_entrypoint = VAEntrypointEncSlice;
  512. ctx->va_rt_format = VA_RT_FORMAT_YUV420;
  513. ctx->va_rc_mode = VA_RC_CQP;
  514. ctx->va_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
  515. VA_ENC_PACKED_HEADER_PICTURE;
  516. ctx->surface_width = FFALIGN(avctx->width, 16);
  517. ctx->surface_height = FFALIGN(avctx->height, 16);
  518. return ff_vaapi_encode_init(avctx);
  519. }
  520. static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
  521. {
  522. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  523. ff_cbs_close(&priv->cbc);
  524. return ff_vaapi_encode_close(avctx);
  525. }
  526. static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = {
  527. { "profile", "4" },
  528. { "level", "4" },
  529. { "bf", "1" },
  530. { "g", "120" },
  531. { "i_qfactor", "1" },
  532. { "i_qoffset", "0" },
  533. { "b_qfactor", "6/5" },
  534. { "b_qoffset", "0" },
  535. { "global_quality", "10" },
  536. { NULL },
  537. };
  538. AVCodec ff_mpeg2_vaapi_encoder = {
  539. .name = "mpeg2_vaapi",
  540. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 (VAAPI)"),
  541. .type = AVMEDIA_TYPE_VIDEO,
  542. .id = AV_CODEC_ID_MPEG2VIDEO,
  543. .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
  544. .init = &vaapi_encode_mpeg2_init,
  545. .encode2 = &ff_vaapi_encode2,
  546. .close = &vaapi_encode_mpeg2_close,
  547. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  548. .defaults = vaapi_encode_mpeg2_defaults,
  549. .pix_fmts = (const enum AVPixelFormat[]) {
  550. AV_PIX_FMT_VAAPI,
  551. AV_PIX_FMT_NONE,
  552. },
  553. .wrapper_name = "vaapi",
  554. };