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.

711 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. int err;
  78. err = ff_cbs_insert_unit_content(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_reset(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_reset(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 (ctx->va_bit_rate > 0) {
  153. priv->bit_rate = (ctx->va_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. // Unspecified video format, from table 6-6.
  240. sde->video_format = 5;
  241. sde->colour_primaries = avctx->color_primaries;
  242. sde->transfer_characteristics = avctx->color_trc;
  243. sde->matrix_coefficients = avctx->colorspace;
  244. sde->colour_description =
  245. avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  246. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  247. avctx->colorspace != AVCOL_SPC_UNSPECIFIED;
  248. sde->display_horizontal_size = avctx->width;
  249. sde->display_vertical_size = avctx->height;
  250. // GOP header
  251. goph->group_start_code = MPEG2_START_GROUP;
  252. // Marker bit in the middle of time_code.
  253. goph->time_code = 1 << 12;
  254. goph->closed_gop = 1;
  255. goph->broken_link = 0;
  256. // Defaults for picture header
  257. ph->picture_start_code = MPEG2_START_PICTURE;
  258. ph->vbv_delay = 0xffff; // Not currently calculated.
  259. ph->full_pel_forward_vector = 0;
  260. ph->forward_f_code = 7;
  261. ph->full_pel_backward_vector = 0;
  262. ph->forward_f_code = 7;
  263. // Defaults for picture coding extension
  264. priv->picture_coding_extension.extension_start_code =
  265. MPEG2_START_EXTENSION;
  266. priv->picture_coding_extension.extension_start_code_identifier =
  267. MPEG2_EXTENSION_PICTURE_CODING;
  268. pce->intra_dc_precision = 0;
  269. pce->picture_structure = 3;
  270. pce->top_field_first = 0;
  271. pce->frame_pred_frame_dct = 1;
  272. pce->concealment_motion_vectors = 0;
  273. pce->q_scale_type = 0;
  274. pce->intra_vlc_format = 0;
  275. pce->alternate_scan = 0;
  276. pce->repeat_first_field = 0;
  277. pce->progressive_frame = 1;
  278. pce->composite_display_flag = 0;
  279. *vseq = (VAEncSequenceParameterBufferMPEG2) {
  280. .intra_period = ctx->gop_size,
  281. .ip_period = ctx->b_per_p + 1,
  282. .picture_width = avctx->width,
  283. .picture_height = avctx->height,
  284. .bits_per_second = ctx->va_bit_rate,
  285. .frame_rate = av_q2d(priv->frame_rate),
  286. .aspect_ratio_information = sh->aspect_ratio_information,
  287. .vbv_buffer_size = priv->vbv_buffer_size,
  288. .sequence_extension.bits = {
  289. .profile_and_level_indication = se->profile_and_level_indication,
  290. .progressive_sequence = se->progressive_sequence,
  291. .chroma_format = se->chroma_format,
  292. .low_delay = se->low_delay,
  293. .frame_rate_extension_n = se->frame_rate_extension_n,
  294. .frame_rate_extension_d = se->frame_rate_extension_d,
  295. },
  296. .new_gop_header = 1,
  297. .gop_header.bits = {
  298. .time_code = goph->time_code,
  299. .closed_gop = goph->closed_gop,
  300. .broken_link = goph->broken_link,
  301. },
  302. };
  303. *vpic = (VAEncPictureParameterBufferMPEG2) {
  304. .forward_reference_picture = VA_INVALID_ID,
  305. .backward_reference_picture = VA_INVALID_ID,
  306. .reconstructed_picture = VA_INVALID_ID,
  307. .coded_buf = VA_INVALID_ID,
  308. .vbv_delay = 0xffff,
  309. .f_code = { { 15, 15 }, { 15, 15 } },
  310. .picture_coding_extension.bits = {
  311. .intra_dc_precision = pce->intra_dc_precision,
  312. .picture_structure = pce->picture_structure,
  313. .top_field_first = pce->top_field_first,
  314. .frame_pred_frame_dct = pce->frame_pred_frame_dct,
  315. .concealment_motion_vectors = pce->concealment_motion_vectors,
  316. .q_scale_type = pce->q_scale_type,
  317. .intra_vlc_format = pce->intra_vlc_format,
  318. .alternate_scan = pce->alternate_scan,
  319. .repeat_first_field = pce->repeat_first_field,
  320. .progressive_frame = pce->progressive_frame,
  321. .composite_display_flag = pce->composite_display_flag,
  322. },
  323. .composite_display.bits = {
  324. .v_axis = pce->v_axis,
  325. .field_sequence = pce->field_sequence,
  326. .sub_carrier = pce->sub_carrier,
  327. .burst_amplitude = pce->burst_amplitude,
  328. .sub_carrier_phase = pce->sub_carrier_phase,
  329. },
  330. };
  331. return 0;
  332. }
  333. static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
  334. VAAPIEncodePicture *pic)
  335. {
  336. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  337. MPEG2RawPictureHeader *ph = &priv->picture_header;
  338. MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
  339. VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params;
  340. if (pic->type == PICTURE_TYPE_IDR || pic->type == PICTURE_TYPE_I) {
  341. ph->temporal_reference = 0;
  342. ph->picture_coding_type = 1;
  343. priv->last_i_frame = pic->display_order;
  344. } else {
  345. ph->temporal_reference = pic->display_order - priv->last_i_frame;
  346. ph->picture_coding_type = pic->type == PICTURE_TYPE_B ? 3 : 2;
  347. }
  348. if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
  349. pce->f_code[0][0] = priv->f_code_horizontal;
  350. pce->f_code[0][1] = priv->f_code_vertical;
  351. } else {
  352. pce->f_code[0][0] = 15;
  353. pce->f_code[0][1] = 15;
  354. }
  355. if (pic->type == PICTURE_TYPE_B) {
  356. pce->f_code[1][0] = priv->f_code_horizontal;
  357. pce->f_code[1][1] = priv->f_code_vertical;
  358. } else {
  359. pce->f_code[1][0] = 15;
  360. pce->f_code[1][1] = 15;
  361. }
  362. vpic->reconstructed_picture = pic->recon_surface;
  363. vpic->coded_buf = pic->output_buffer;
  364. switch (pic->type) {
  365. case PICTURE_TYPE_IDR:
  366. case PICTURE_TYPE_I:
  367. vpic->picture_type = VAEncPictureTypeIntra;
  368. break;
  369. case PICTURE_TYPE_P:
  370. vpic->picture_type = VAEncPictureTypePredictive;
  371. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  372. break;
  373. case PICTURE_TYPE_B:
  374. vpic->picture_type = VAEncPictureTypeBidirectional;
  375. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  376. vpic->backward_reference_picture = pic->refs[1]->recon_surface;
  377. break;
  378. default:
  379. av_assert0(0 && "invalid picture type");
  380. }
  381. vpic->temporal_reference = ph->temporal_reference;
  382. vpic->f_code[0][0] = pce->f_code[0][0];
  383. vpic->f_code[0][1] = pce->f_code[0][1];
  384. vpic->f_code[1][0] = pce->f_code[1][0];
  385. vpic->f_code[1][1] = pce->f_code[1][1];
  386. return 0;
  387. }
  388. static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
  389. VAAPIEncodePicture *pic,
  390. VAAPIEncodeSlice *slice)
  391. {
  392. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  393. VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
  394. int qp;
  395. vslice->macroblock_address = slice->block_start;
  396. vslice->num_macroblocks = slice->block_size;
  397. switch (pic->type) {
  398. case PICTURE_TYPE_IDR:
  399. case PICTURE_TYPE_I:
  400. qp = priv->quant_i;
  401. break;
  402. case PICTURE_TYPE_P:
  403. qp = priv->quant_p;
  404. break;
  405. case PICTURE_TYPE_B:
  406. qp = priv->quant_b;
  407. break;
  408. default:
  409. av_assert0(0 && "invalid picture type");
  410. }
  411. vslice->quantiser_scale_code = qp;
  412. vslice->is_intra_slice = (pic->type == PICTURE_TYPE_IDR ||
  413. pic->type == PICTURE_TYPE_I);
  414. return 0;
  415. }
  416. static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
  417. {
  418. VAAPIEncodeContext *ctx = avctx->priv_data;
  419. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  420. int err;
  421. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
  422. if (err < 0)
  423. return err;
  424. if (ctx->va_rc_mode == VA_RC_CQP) {
  425. priv->quant_p = av_clip(ctx->rc_quality, 1, 31);
  426. if (avctx->i_quant_factor > 0.0)
  427. priv->quant_i =
  428. av_clip((avctx->i_quant_factor * priv->quant_p +
  429. avctx->i_quant_offset) + 0.5, 1, 31);
  430. else
  431. priv->quant_i = priv->quant_p;
  432. if (avctx->b_quant_factor > 0.0)
  433. priv->quant_b =
  434. av_clip((avctx->b_quant_factor * priv->quant_p +
  435. avctx->b_quant_offset) + 0.5, 1, 31);
  436. else
  437. priv->quant_b = priv->quant_p;
  438. av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
  439. "%d / %d / %d for I- / P- / B-frames.\n",
  440. priv->quant_i, priv->quant_p, priv->quant_b);
  441. } else {
  442. priv->quant_i = 16;
  443. priv->quant_p = 16;
  444. priv->quant_b = 16;
  445. }
  446. ctx->slice_block_rows = FFALIGN(avctx->height, 16) / 16;
  447. ctx->slice_block_cols = FFALIGN(avctx->width, 16) / 16;
  448. ctx->nb_slices = ctx->slice_block_rows;
  449. ctx->slice_size = 1;
  450. ctx->roi_quant_range = 31;
  451. return 0;
  452. }
  453. static const VAAPIEncodeProfile vaapi_encode_mpeg2_profiles[] = {
  454. { FF_PROFILE_MPEG2_MAIN, 8, 3, 1, 1, VAProfileMPEG2Main },
  455. { FF_PROFILE_MPEG2_SIMPLE, 8, 3, 1, 1, VAProfileMPEG2Simple },
  456. { FF_PROFILE_UNKNOWN }
  457. };
  458. static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
  459. .profiles = vaapi_encode_mpeg2_profiles,
  460. .flags = FLAG_B_PICTURES,
  461. .configure = &vaapi_encode_mpeg2_configure,
  462. .default_quality = 10,
  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_fragment_free(&priv->current_fragment);
  511. ff_cbs_close(&priv->cbc);
  512. return ff_vaapi_encode_close(avctx);
  513. }
  514. #define OFFSET(x) offsetof(VAAPIEncodeMPEG2Context, x)
  515. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  516. static const AVOption vaapi_encode_mpeg2_options[] = {
  517. VAAPI_ENCODE_COMMON_OPTIONS,
  518. VAAPI_ENCODE_RC_OPTIONS,
  519. { "profile", "Set profile (in profile_and_level_indication)",
  520. OFFSET(profile), AV_OPT_TYPE_INT,
  521. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 7, FLAGS, "profile" },
  522. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  523. { .i64 = value }, 0, 0, FLAGS, "profile"
  524. { PROFILE("simple", FF_PROFILE_MPEG2_SIMPLE) },
  525. { PROFILE("main", FF_PROFILE_MPEG2_MAIN) },
  526. #undef PROFILE
  527. { "level", "Set level (in profile_and_level_indication)",
  528. OFFSET(level), AV_OPT_TYPE_INT,
  529. { .i64 = 4 }, 0, 15, FLAGS, "level" },
  530. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  531. { .i64 = value }, 0, 0, FLAGS, "level"
  532. { LEVEL("low", 10) },
  533. { LEVEL("main", 8) },
  534. { LEVEL("high_1440", 6) },
  535. { LEVEL("high", 4) },
  536. #undef LEVEL
  537. { NULL },
  538. };
  539. static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = {
  540. { "b", "0" },
  541. { "bf", "1" },
  542. { "g", "120" },
  543. { "i_qfactor", "1" },
  544. { "i_qoffset", "0" },
  545. { "b_qfactor", "6/5" },
  546. { "b_qoffset", "0" },
  547. { "qmin", "-1" },
  548. { "qmax", "-1" },
  549. { NULL },
  550. };
  551. static const AVClass vaapi_encode_mpeg2_class = {
  552. .class_name = "mpeg2_vaapi",
  553. .item_name = av_default_item_name,
  554. .option = vaapi_encode_mpeg2_options,
  555. .version = LIBAVUTIL_VERSION_INT,
  556. };
  557. AVCodec ff_mpeg2_vaapi_encoder = {
  558. .name = "mpeg2_vaapi",
  559. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 (VAAPI)"),
  560. .type = AVMEDIA_TYPE_VIDEO,
  561. .id = AV_CODEC_ID_MPEG2VIDEO,
  562. .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
  563. .init = &vaapi_encode_mpeg2_init,
  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. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  569. .defaults = vaapi_encode_mpeg2_defaults,
  570. .pix_fmts = (const enum AVPixelFormat[]) {
  571. AV_PIX_FMT_VAAPI,
  572. AV_PIX_FMT_NONE,
  573. },
  574. .hw_configs = ff_vaapi_encode_hw_configs,
  575. .wrapper_name = "vaapi",
  576. };