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.

706 lines
24KB

  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. // User options.
  29. int profile;
  30. int level;
  31. // Derived settings.
  32. int quant_i;
  33. int quant_p;
  34. int quant_b;
  35. unsigned int bit_rate;
  36. unsigned int vbv_buffer_size;
  37. AVRational frame_rate;
  38. unsigned int f_code_horizontal;
  39. unsigned int f_code_vertical;
  40. // Stream state.
  41. int64_t last_i_frame;
  42. // Writer structures.
  43. MPEG2RawSequenceHeader sequence_header;
  44. MPEG2RawExtensionData sequence_extension;
  45. MPEG2RawExtensionData sequence_display_extension;
  46. MPEG2RawGroupOfPicturesHeader gop_header;
  47. MPEG2RawPictureHeader picture_header;
  48. MPEG2RawExtensionData picture_coding_extension;
  49. CodedBitstreamContext *cbc;
  50. CodedBitstreamFragment current_fragment;
  51. } VAAPIEncodeMPEG2Context;
  52. static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx,
  53. char *data, size_t *data_len,
  54. CodedBitstreamFragment *frag)
  55. {
  56. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  57. int err;
  58. err = ff_cbs_write_fragment_data(priv->cbc, frag);
  59. if (err < 0) {
  60. av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
  61. return err;
  62. }
  63. if (*data_len < 8 * frag->data_size - frag->data_bit_padding) {
  64. av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
  65. "%zu < %zu.\n", *data_len,
  66. 8 * frag->data_size - frag->data_bit_padding);
  67. return AVERROR(ENOSPC);
  68. }
  69. memcpy(data, frag->data, frag->data_size);
  70. *data_len = 8 * frag->data_size - frag->data_bit_padding;
  71. return 0;
  72. }
  73. static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx,
  74. CodedBitstreamFragment *frag,
  75. int type, void *header)
  76. {
  77. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  78. int err;
  79. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1, type, header, NULL);
  80. if (err < 0) {
  81. av_log(avctx, AV_LOG_ERROR, "Failed to add header: "
  82. "type = %d.\n", type);
  83. return err;
  84. }
  85. return 0;
  86. }
  87. static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx,
  88. char *data, size_t *data_len)
  89. {
  90. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  91. CodedBitstreamFragment *frag = &priv->current_fragment;
  92. int err;
  93. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_SEQUENCE_HEADER,
  94. &priv->sequence_header);
  95. if (err < 0)
  96. goto fail;
  97. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  98. &priv->sequence_extension);
  99. if (err < 0)
  100. goto fail;
  101. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  102. &priv->sequence_display_extension);
  103. if (err < 0)
  104. goto fail;
  105. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_GROUP,
  106. &priv->gop_header);
  107. if (err < 0)
  108. goto fail;
  109. err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
  110. fail:
  111. ff_cbs_fragment_uninit(priv->cbc, frag);
  112. return 0;
  113. }
  114. static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx,
  115. VAAPIEncodePicture *pic,
  116. char *data, size_t *data_len)
  117. {
  118. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  119. CodedBitstreamFragment *frag = &priv->current_fragment;
  120. int err;
  121. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_PICTURE,
  122. &priv->picture_header);
  123. if (err < 0)
  124. goto fail;
  125. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  126. &priv->picture_coding_extension);
  127. if (err < 0)
  128. goto fail;
  129. err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
  130. fail:
  131. ff_cbs_fragment_uninit(priv->cbc, frag);
  132. return 0;
  133. }
  134. static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
  135. {
  136. VAAPIEncodeContext *ctx = avctx->priv_data;
  137. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  138. MPEG2RawSequenceHeader *sh = &priv->sequence_header;
  139. MPEG2RawSequenceExtension *se = &priv->sequence_extension.data.sequence;
  140. MPEG2RawSequenceDisplayExtension *sde = &priv->sequence_display_extension.data.sequence_display;
  141. MPEG2RawGroupOfPicturesHeader *goph = &priv->gop_header;
  142. MPEG2RawPictureHeader *ph = &priv->picture_header;
  143. MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
  144. VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params;
  145. VAEncPictureParameterBufferMPEG2 *vpic = ctx->codec_picture_params;
  146. int code, ext_n, ext_d;
  147. memset(sh, 0, sizeof(*sh));
  148. memset(se, 0, sizeof(*se));
  149. memset(sde, 0, sizeof(*sde));
  150. memset(goph, 0, sizeof(*goph));
  151. memset(ph, 0, sizeof(*ph));
  152. memset(pce, 0, sizeof(*pce));
  153. if (ctx->va_bit_rate > 0) {
  154. priv->bit_rate = (ctx->va_bit_rate + 399) / 400;
  155. } else {
  156. // Unknown (not a bitrate-targetting mode), so just use the
  157. // highest value.
  158. priv->bit_rate = 0x3fffffff;
  159. }
  160. if (avctx->rc_buffer_size > 0) {
  161. priv->vbv_buffer_size = (avctx->rc_buffer_size + (1 << 14) - 1) >> 14;
  162. } else {
  163. // Unknown, so guess a value from the bitrate.
  164. priv->vbv_buffer_size = priv->bit_rate >> 14;
  165. }
  166. switch (avctx->level) {
  167. case 4: // High.
  168. case 6: // High 1440.
  169. priv->f_code_horizontal = 9;
  170. priv->f_code_vertical = 5;
  171. break;
  172. case 8: // Main.
  173. priv->f_code_horizontal = 8;
  174. priv->f_code_vertical = 5;
  175. break;
  176. case 10: // Low.
  177. default:
  178. priv->f_code_horizontal = 7;
  179. priv->f_code_vertical = 4;
  180. break;
  181. }
  182. // Sequence header
  183. sh->sequence_header_code = MPEG2_START_SEQUENCE_HEADER;
  184. sh->horizontal_size_value = avctx->width & 0xfff;
  185. sh->vertical_size_value = avctx->height & 0xfff;
  186. if (avctx->sample_aspect_ratio.num != 0 &&
  187. avctx->sample_aspect_ratio.den != 0) {
  188. AVRational dar = av_div_q(avctx->sample_aspect_ratio,
  189. (AVRational) { avctx->width, avctx->height });
  190. if (av_cmp_q(avctx->sample_aspect_ratio, (AVRational) { 1, 1 }) == 0) {
  191. sh->aspect_ratio_information = 1;
  192. } else if (av_cmp_q(dar, (AVRational) { 3, 4 }) == 0) {
  193. sh->aspect_ratio_information = 2;
  194. } else if (av_cmp_q(dar, (AVRational) { 9, 16 }) == 0) {
  195. sh->aspect_ratio_information = 3;
  196. } else if (av_cmp_q(dar, (AVRational) { 100, 221 }) == 0) {
  197. sh->aspect_ratio_information = 4;
  198. } else {
  199. av_log(avctx, AV_LOG_WARNING, "Sample aspect ratio %d:%d is not "
  200. "representable, signalling square pixels instead.\n",
  201. avctx->sample_aspect_ratio.num,
  202. avctx->sample_aspect_ratio.den);
  203. sh->aspect_ratio_information = 1;
  204. }
  205. } else {
  206. // Unknown - assume square pixels.
  207. sh->aspect_ratio_information = 1;
  208. }
  209. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  210. priv->frame_rate = avctx->framerate;
  211. else
  212. priv->frame_rate = av_inv_q(avctx->time_base);
  213. ff_mpeg12_find_best_frame_rate(priv->frame_rate,
  214. &code, &ext_n, &ext_d, 0);
  215. sh->frame_rate_code = code;
  216. sh->bit_rate_value = priv->bit_rate & 0x3ffff;
  217. sh->vbv_buffer_size_value = priv->vbv_buffer_size & 0x3ff;
  218. sh->constrained_parameters_flag = 0;
  219. sh->load_intra_quantiser_matrix = 0;
  220. sh->load_non_intra_quantiser_matrix = 0;
  221. // Sequence extension
  222. priv->sequence_extension.extension_start_code = MPEG2_START_EXTENSION;
  223. priv->sequence_extension.extension_start_code_identifier =
  224. MPEG2_EXTENSION_SEQUENCE;
  225. se->profile_and_level_indication = avctx->profile << 4 | avctx->level;
  226. se->progressive_sequence = 1;
  227. se->chroma_format = 1;
  228. se->horizontal_size_extension = avctx->width >> 12;
  229. se->vertical_size_extension = avctx->height >> 12;
  230. se->bit_rate_extension = priv->bit_rate >> 18;
  231. se->vbv_buffer_size_extension = priv->vbv_buffer_size >> 10;
  232. se->low_delay = ctx->b_per_p == 0;
  233. se->frame_rate_extension_n = ext_n;
  234. se->frame_rate_extension_d = ext_d;
  235. // Sequence display extension
  236. priv->sequence_display_extension.extension_start_code =
  237. MPEG2_START_EXTENSION;
  238. priv->sequence_display_extension.extension_start_code_identifier =
  239. MPEG2_EXTENSION_SEQUENCE_DISPLAY;
  240. sde->video_format = 5;
  241. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  242. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  243. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  244. sde->colour_description = 1;
  245. sde->colour_primaries = avctx->color_primaries;
  246. sde->transfer_characteristics = avctx->color_trc;
  247. sde->matrix_coefficients = avctx->colorspace;
  248. } else {
  249. sde->colour_description = 0;
  250. }
  251. sde->display_horizontal_size = avctx->width;
  252. sde->display_vertical_size = avctx->height;
  253. // GOP header
  254. goph->group_start_code = MPEG2_START_GROUP;
  255. goph->time_code = 0;
  256. goph->closed_gop = 1;
  257. goph->broken_link = 0;
  258. // Defaults for picture header
  259. ph->picture_start_code = MPEG2_START_PICTURE;
  260. ph->vbv_delay = 0xffff; // Not currently calculated.
  261. ph->full_pel_forward_vector = 0;
  262. ph->forward_f_code = 7;
  263. ph->full_pel_backward_vector = 0;
  264. ph->forward_f_code = 7;
  265. // Defaults for picture coding extension
  266. priv->picture_coding_extension.extension_start_code =
  267. MPEG2_START_EXTENSION;
  268. priv->picture_coding_extension.extension_start_code_identifier =
  269. MPEG2_EXTENSION_PICTURE_CODING;
  270. pce->intra_dc_precision = 0;
  271. pce->picture_structure = 3;
  272. pce->top_field_first = 0;
  273. pce->frame_pred_frame_dct = 1;
  274. pce->concealment_motion_vectors = 0;
  275. pce->q_scale_type = 0;
  276. pce->intra_vlc_format = 0;
  277. pce->alternate_scan = 0;
  278. pce->repeat_first_field = 0;
  279. pce->progressive_frame = 1;
  280. pce->composite_display_flag = 0;
  281. *vseq = (VAEncSequenceParameterBufferMPEG2) {
  282. .intra_period = ctx->gop_size,
  283. .ip_period = ctx->b_per_p + 1,
  284. .picture_width = avctx->width,
  285. .picture_height = avctx->height,
  286. .bits_per_second = ctx->va_bit_rate,
  287. .frame_rate = av_q2d(priv->frame_rate),
  288. .aspect_ratio_information = sh->aspect_ratio_information,
  289. .vbv_buffer_size = priv->vbv_buffer_size,
  290. .sequence_extension.bits = {
  291. .profile_and_level_indication = se->profile_and_level_indication,
  292. .progressive_sequence = se->progressive_sequence,
  293. .chroma_format = se->chroma_format,
  294. .low_delay = se->low_delay,
  295. .frame_rate_extension_n = se->frame_rate_extension_n,
  296. .frame_rate_extension_d = se->frame_rate_extension_d,
  297. },
  298. .new_gop_header = 1,
  299. .gop_header.bits = {
  300. .time_code = goph->time_code,
  301. .closed_gop = goph->closed_gop,
  302. .broken_link = goph->broken_link,
  303. },
  304. };
  305. *vpic = (VAEncPictureParameterBufferMPEG2) {
  306. .forward_reference_picture = VA_INVALID_ID,
  307. .backward_reference_picture = VA_INVALID_ID,
  308. .reconstructed_picture = VA_INVALID_ID,
  309. .coded_buf = VA_INVALID_ID,
  310. .vbv_delay = 0xffff,
  311. .f_code = { { 15, 15 }, { 15, 15 } },
  312. .picture_coding_extension.bits = {
  313. .intra_dc_precision = pce->intra_dc_precision,
  314. .picture_structure = pce->picture_structure,
  315. .top_field_first = pce->top_field_first,
  316. .frame_pred_frame_dct = pce->frame_pred_frame_dct,
  317. .concealment_motion_vectors = pce->concealment_motion_vectors,
  318. .q_scale_type = pce->q_scale_type,
  319. .intra_vlc_format = pce->intra_vlc_format,
  320. .alternate_scan = pce->alternate_scan,
  321. .repeat_first_field = pce->repeat_first_field,
  322. .progressive_frame = pce->progressive_frame,
  323. .composite_display_flag = pce->composite_display_flag,
  324. },
  325. .composite_display.bits = {
  326. .v_axis = pce->v_axis,
  327. .field_sequence = pce->field_sequence,
  328. .sub_carrier = pce->sub_carrier,
  329. .burst_amplitude = pce->burst_amplitude,
  330. .sub_carrier_phase = pce->sub_carrier_phase,
  331. },
  332. };
  333. return 0;
  334. }
  335. static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
  336. VAAPIEncodePicture *pic)
  337. {
  338. VAAPIEncodeMPEG2Context *priv = avctx->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. 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 = slice->block_start;
  398. vslice->num_macroblocks = slice->block_size;
  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. if (ctx->va_rc_mode == VA_RC_CQP) {
  427. priv->quant_p = av_clip(avctx->global_quality, 1, 31);
  428. if (avctx->i_quant_factor > 0.0)
  429. priv->quant_i = av_clip((avctx->global_quality *
  430. avctx->i_quant_factor +
  431. avctx->i_quant_offset) + 0.5,
  432. 1, 31);
  433. else
  434. priv->quant_i = priv->quant_p;
  435. if (avctx->b_quant_factor > 0.0)
  436. priv->quant_b = av_clip((avctx->global_quality *
  437. avctx->b_quant_factor +
  438. avctx->b_quant_offset) + 0.5,
  439. 1, 31);
  440. else
  441. priv->quant_b = priv->quant_p;
  442. av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
  443. "%d / %d / %d for I- / P- / B-frames.\n",
  444. priv->quant_i, priv->quant_p, priv->quant_b);
  445. } else {
  446. av_assert0(0 && "Invalid RC mode.");
  447. }
  448. ctx->slice_block_rows = FFALIGN(avctx->height, 16) / 16;
  449. ctx->slice_block_cols = FFALIGN(avctx->width, 16) / 16;
  450. ctx->nb_slices = ctx->slice_block_rows;
  451. ctx->slice_size = 1;
  452. return 0;
  453. }
  454. static const VAAPIEncodeProfile vaapi_encode_mpeg2_profiles[] = {
  455. { FF_PROFILE_MPEG2_MAIN, 8, 3, 1, 1, VAProfileMPEG2Main },
  456. { FF_PROFILE_MPEG2_SIMPLE, 8, 3, 1, 1, VAProfileMPEG2Simple },
  457. { FF_PROFILE_UNKNOWN }
  458. };
  459. static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
  460. .profiles = vaapi_encode_mpeg2_profiles,
  461. .flags = FLAG_B_PICTURES,
  462. .configure = &vaapi_encode_mpeg2_configure,
  463. .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
  464. .init_sequence_params = &vaapi_encode_mpeg2_init_sequence_params,
  465. .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
  466. .init_picture_params = &vaapi_encode_mpeg2_init_picture_params,
  467. .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
  468. .init_slice_params = &vaapi_encode_mpeg2_init_slice_params,
  469. .sequence_header_type = VAEncPackedHeaderSequence,
  470. .write_sequence_header = &vaapi_encode_mpeg2_write_sequence_header,
  471. .picture_header_type = VAEncPackedHeaderPicture,
  472. .write_picture_header = &vaapi_encode_mpeg2_write_picture_header,
  473. };
  474. static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
  475. {
  476. VAAPIEncodeContext *ctx = avctx->priv_data;
  477. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  478. ctx->codec = &vaapi_encode_type_mpeg2;
  479. if (avctx->profile == FF_PROFILE_UNKNOWN)
  480. avctx->profile = priv->profile;
  481. if (avctx->level == FF_LEVEL_UNKNOWN)
  482. avctx->level = priv->level;
  483. // Reject unknown levels (these are required to set f_code for
  484. // motion vector encoding).
  485. switch (avctx->level) {
  486. case 4: // High
  487. case 6: // High 1440
  488. case 8: // Main
  489. case 10: // Low
  490. break;
  491. default:
  492. av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 level %d.\n",
  493. avctx->level);
  494. return AVERROR(EINVAL);
  495. }
  496. if (avctx->height % 4096 == 0 || avctx->width % 4096 == 0) {
  497. av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support picture "
  498. "height or width divisible by 4096.\n");
  499. return AVERROR(EINVAL);
  500. }
  501. ctx->desired_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
  502. VA_ENC_PACKED_HEADER_PICTURE;
  503. ctx->surface_width = FFALIGN(avctx->width, 16);
  504. ctx->surface_height = FFALIGN(avctx->height, 16);
  505. return ff_vaapi_encode_init(avctx);
  506. }
  507. static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
  508. {
  509. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  510. ff_cbs_close(&priv->cbc);
  511. return ff_vaapi_encode_close(avctx);
  512. }
  513. #define OFFSET(x) offsetof(VAAPIEncodeMPEG2Context, x)
  514. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  515. static const AVOption vaapi_encode_mpeg2_options[] = {
  516. VAAPI_ENCODE_COMMON_OPTIONS,
  517. { "profile", "Set profile (in profile_and_level_indication)",
  518. OFFSET(profile), AV_OPT_TYPE_INT,
  519. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 7, FLAGS, "profile" },
  520. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  521. { .i64 = value }, 0, 0, FLAGS, "profile"
  522. { PROFILE("simple", FF_PROFILE_MPEG2_SIMPLE) },
  523. { PROFILE("main", FF_PROFILE_MPEG2_MAIN) },
  524. #undef PROFILE
  525. { "level", "Set level (in profile_and_level_indication)",
  526. OFFSET(level), AV_OPT_TYPE_INT,
  527. { .i64 = 4 }, 0, 15, FLAGS, "level" },
  528. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  529. { .i64 = value }, 0, 0, FLAGS, "level"
  530. { LEVEL("low", 10) },
  531. { LEVEL("main", 8) },
  532. { LEVEL("high_1440", 6) },
  533. { LEVEL("high", 4) },
  534. #undef LEVEL
  535. { NULL },
  536. };
  537. static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = {
  538. { "b", "0" },
  539. { "bf", "1" },
  540. { "g", "120" },
  541. { "i_qfactor", "1" },
  542. { "i_qoffset", "0" },
  543. { "b_qfactor", "6/5" },
  544. { "b_qoffset", "0" },
  545. { "global_quality", "10" },
  546. { "qmin", "-1" },
  547. { "qmax", "-1" },
  548. { NULL },
  549. };
  550. static const AVClass vaapi_encode_mpeg2_class = {
  551. .class_name = "mpeg2_vaapi",
  552. .item_name = av_default_item_name,
  553. .option = vaapi_encode_mpeg2_options,
  554. .version = LIBAVUTIL_VERSION_INT,
  555. };
  556. AVCodec ff_mpeg2_vaapi_encoder = {
  557. .name = "mpeg2_vaapi",
  558. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 (VAAPI)"),
  559. .type = AVMEDIA_TYPE_VIDEO,
  560. .id = AV_CODEC_ID_MPEG2VIDEO,
  561. .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
  562. .init = &vaapi_encode_mpeg2_init,
  563. .send_frame = &ff_vaapi_encode_send_frame,
  564. .receive_packet = &ff_vaapi_encode_receive_packet,
  565. .close = &vaapi_encode_mpeg2_close,
  566. .priv_class = &vaapi_encode_mpeg2_class,
  567. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  568. .defaults = vaapi_encode_mpeg2_defaults,
  569. .pix_fmts = (const enum AVPixelFormat[]) {
  570. AV_PIX_FMT_VAAPI,
  571. AV_PIX_FMT_NONE,
  572. },
  573. .wrapper_name = "vaapi",
  574. };