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.

705 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 mb_width;
  33. int mb_height;
  34. int quant_i;
  35. int quant_p;
  36. int quant_b;
  37. unsigned int bit_rate;
  38. unsigned int vbv_buffer_size;
  39. AVRational frame_rate;
  40. unsigned int f_code_horizontal;
  41. unsigned int f_code_vertical;
  42. // Stream state.
  43. int64_t last_i_frame;
  44. // Writer structures.
  45. MPEG2RawSequenceHeader sequence_header;
  46. MPEG2RawExtensionData sequence_extension;
  47. MPEG2RawExtensionData sequence_display_extension;
  48. MPEG2RawGroupOfPicturesHeader gop_header;
  49. MPEG2RawPictureHeader picture_header;
  50. MPEG2RawExtensionData picture_coding_extension;
  51. CodedBitstreamContext *cbc;
  52. CodedBitstreamFragment current_fragment;
  53. } VAAPIEncodeMPEG2Context;
  54. static int vaapi_encode_mpeg2_write_fragment(AVCodecContext *avctx,
  55. char *data, size_t *data_len,
  56. CodedBitstreamFragment *frag)
  57. {
  58. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  59. int err;
  60. err = ff_cbs_write_fragment_data(priv->cbc, frag);
  61. if (err < 0) {
  62. av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
  63. return err;
  64. }
  65. if (*data_len < 8 * frag->data_size - frag->data_bit_padding) {
  66. av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
  67. "%zu < %zu.\n", *data_len,
  68. 8 * frag->data_size - frag->data_bit_padding);
  69. return AVERROR(ENOSPC);
  70. }
  71. memcpy(data, frag->data, frag->data_size);
  72. *data_len = 8 * frag->data_size - frag->data_bit_padding;
  73. return 0;
  74. }
  75. static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx,
  76. CodedBitstreamFragment *frag,
  77. int type, void *header)
  78. {
  79. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  80. int err;
  81. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1, type, header, NULL);
  82. if (err < 0) {
  83. av_log(avctx, AV_LOG_ERROR, "Failed to add header: "
  84. "type = %d.\n", type);
  85. return err;
  86. }
  87. return 0;
  88. }
  89. static int vaapi_encode_mpeg2_write_sequence_header(AVCodecContext *avctx,
  90. char *data, size_t *data_len)
  91. {
  92. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  93. CodedBitstreamFragment *frag = &priv->current_fragment;
  94. int err;
  95. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_SEQUENCE_HEADER,
  96. &priv->sequence_header);
  97. if (err < 0)
  98. goto fail;
  99. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  100. &priv->sequence_extension);
  101. if (err < 0)
  102. goto fail;
  103. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  104. &priv->sequence_display_extension);
  105. if (err < 0)
  106. goto fail;
  107. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_GROUP,
  108. &priv->gop_header);
  109. if (err < 0)
  110. goto fail;
  111. err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
  112. fail:
  113. ff_cbs_fragment_uninit(priv->cbc, frag);
  114. return 0;
  115. }
  116. static int vaapi_encode_mpeg2_write_picture_header(AVCodecContext *avctx,
  117. VAAPIEncodePicture *pic,
  118. char *data, size_t *data_len)
  119. {
  120. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  121. CodedBitstreamFragment *frag = &priv->current_fragment;
  122. int err;
  123. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_PICTURE,
  124. &priv->picture_header);
  125. if (err < 0)
  126. goto fail;
  127. err = vaapi_encode_mpeg2_add_header(avctx, frag, MPEG2_START_EXTENSION,
  128. &priv->picture_coding_extension);
  129. if (err < 0)
  130. goto fail;
  131. err = vaapi_encode_mpeg2_write_fragment(avctx, data, data_len, frag);
  132. fail:
  133. ff_cbs_fragment_uninit(priv->cbc, frag);
  134. return 0;
  135. }
  136. static int vaapi_encode_mpeg2_init_sequence_params(AVCodecContext *avctx)
  137. {
  138. VAAPIEncodeContext *ctx = avctx->priv_data;
  139. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  140. MPEG2RawSequenceHeader *sh = &priv->sequence_header;
  141. MPEG2RawSequenceExtension *se = &priv->sequence_extension.data.sequence;
  142. MPEG2RawSequenceDisplayExtension *sde = &priv->sequence_display_extension.data.sequence_display;
  143. MPEG2RawGroupOfPicturesHeader *goph = &priv->gop_header;
  144. MPEG2RawPictureHeader *ph = &priv->picture_header;
  145. MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
  146. VAEncSequenceParameterBufferMPEG2 *vseq = ctx->codec_sequence_params;
  147. VAEncPictureParameterBufferMPEG2 *vpic = ctx->codec_picture_params;
  148. int code, ext_n, ext_d;
  149. memset(sh, 0, sizeof(*sh));
  150. memset(se, 0, sizeof(*se));
  151. memset(sde, 0, sizeof(*sde));
  152. memset(goph, 0, sizeof(*goph));
  153. memset(ph, 0, sizeof(*ph));
  154. memset(pce, 0, sizeof(*pce));
  155. if (ctx->va_bit_rate > 0) {
  156. priv->bit_rate = (ctx->va_bit_rate + 399) / 400;
  157. } else {
  158. // Unknown (not a bitrate-targetting mode), so just use the
  159. // highest value.
  160. priv->bit_rate = 0x3fffffff;
  161. }
  162. if (avctx->rc_buffer_size > 0) {
  163. priv->vbv_buffer_size = (avctx->rc_buffer_size + (1 << 14) - 1) >> 14;
  164. } else {
  165. // Unknown, so guess a value from the bitrate.
  166. priv->vbv_buffer_size = priv->bit_rate >> 14;
  167. }
  168. switch (avctx->level) {
  169. case 4: // High.
  170. case 6: // High 1440.
  171. priv->f_code_horizontal = 9;
  172. priv->f_code_vertical = 5;
  173. break;
  174. case 8: // Main.
  175. priv->f_code_horizontal = 8;
  176. priv->f_code_vertical = 5;
  177. break;
  178. case 10: // Low.
  179. default:
  180. priv->f_code_horizontal = 7;
  181. priv->f_code_vertical = 4;
  182. break;
  183. }
  184. // Sequence header
  185. sh->sequence_header_code = MPEG2_START_SEQUENCE_HEADER;
  186. sh->horizontal_size_value = avctx->width & 0xfff;
  187. sh->vertical_size_value = avctx->height & 0xfff;
  188. if (avctx->sample_aspect_ratio.num != 0 &&
  189. avctx->sample_aspect_ratio.den != 0) {
  190. AVRational dar = av_div_q(avctx->sample_aspect_ratio,
  191. (AVRational) { avctx->width, avctx->height });
  192. if (av_cmp_q(avctx->sample_aspect_ratio, (AVRational) { 1, 1 }) == 0) {
  193. sh->aspect_ratio_information = 1;
  194. } else if (av_cmp_q(dar, (AVRational) { 3, 4 }) == 0) {
  195. sh->aspect_ratio_information = 2;
  196. } else if (av_cmp_q(dar, (AVRational) { 9, 16 }) == 0) {
  197. sh->aspect_ratio_information = 3;
  198. } else if (av_cmp_q(dar, (AVRational) { 100, 221 }) == 0) {
  199. sh->aspect_ratio_information = 4;
  200. } else {
  201. av_log(avctx, AV_LOG_WARNING, "Sample aspect ratio %d:%d is not "
  202. "representable, signalling square pixels instead.\n",
  203. avctx->sample_aspect_ratio.num,
  204. avctx->sample_aspect_ratio.den);
  205. sh->aspect_ratio_information = 1;
  206. }
  207. } else {
  208. // Unknown - assume square pixels.
  209. sh->aspect_ratio_information = 1;
  210. }
  211. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  212. priv->frame_rate = avctx->framerate;
  213. else
  214. priv->frame_rate = av_inv_q(avctx->time_base);
  215. ff_mpeg12_find_best_frame_rate(priv->frame_rate,
  216. &code, &ext_n, &ext_d, 0);
  217. sh->frame_rate_code = code;
  218. sh->bit_rate_value = priv->bit_rate & 0x3ffff;
  219. sh->vbv_buffer_size_value = priv->vbv_buffer_size & 0x3ff;
  220. sh->constrained_parameters_flag = 0;
  221. sh->load_intra_quantiser_matrix = 0;
  222. sh->load_non_intra_quantiser_matrix = 0;
  223. // Sequence extension
  224. priv->sequence_extension.extension_start_code = MPEG2_START_EXTENSION;
  225. priv->sequence_extension.extension_start_code_identifier =
  226. MPEG2_EXTENSION_SEQUENCE;
  227. se->profile_and_level_indication = avctx->profile << 4 | avctx->level;
  228. se->progressive_sequence = 1;
  229. se->chroma_format = 1;
  230. se->horizontal_size_extension = avctx->width >> 12;
  231. se->vertical_size_extension = avctx->height >> 12;
  232. se->bit_rate_extension = priv->bit_rate >> 18;
  233. se->vbv_buffer_size_extension = priv->vbv_buffer_size >> 10;
  234. se->low_delay = ctx->b_per_p == 0;
  235. se->frame_rate_extension_n = ext_n;
  236. se->frame_rate_extension_d = ext_d;
  237. // Sequence display extension
  238. priv->sequence_display_extension.extension_start_code =
  239. MPEG2_START_EXTENSION;
  240. priv->sequence_display_extension.extension_start_code_identifier =
  241. MPEG2_EXTENSION_SEQUENCE_DISPLAY;
  242. sde->video_format = 5;
  243. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  244. avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
  245. avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
  246. sde->colour_description = 1;
  247. sde->colour_primaries = avctx->color_primaries;
  248. sde->transfer_characteristics = avctx->color_trc;
  249. sde->matrix_coefficients = avctx->colorspace;
  250. } else {
  251. sde->colour_description = 0;
  252. }
  253. sde->display_horizontal_size = avctx->width;
  254. sde->display_vertical_size = avctx->height;
  255. // GOP header
  256. goph->group_start_code = MPEG2_START_GROUP;
  257. goph->time_code = 0;
  258. goph->closed_gop = 1;
  259. goph->broken_link = 0;
  260. // Defaults for picture header
  261. ph->picture_start_code = MPEG2_START_PICTURE;
  262. ph->vbv_delay = 0xffff; // Not currently calculated.
  263. ph->full_pel_forward_vector = 0;
  264. ph->forward_f_code = 7;
  265. ph->full_pel_backward_vector = 0;
  266. ph->forward_f_code = 7;
  267. // Defaults for picture coding extension
  268. priv->picture_coding_extension.extension_start_code =
  269. MPEG2_START_EXTENSION;
  270. priv->picture_coding_extension.extension_start_code_identifier =
  271. MPEG2_EXTENSION_PICTURE_CODING;
  272. pce->intra_dc_precision = 0;
  273. pce->picture_structure = 3;
  274. pce->top_field_first = 0;
  275. pce->frame_pred_frame_dct = 1;
  276. pce->concealment_motion_vectors = 0;
  277. pce->q_scale_type = 0;
  278. pce->intra_vlc_format = 0;
  279. pce->alternate_scan = 0;
  280. pce->repeat_first_field = 0;
  281. pce->progressive_frame = 1;
  282. pce->composite_display_flag = 0;
  283. *vseq = (VAEncSequenceParameterBufferMPEG2) {
  284. .intra_period = ctx->gop_size,
  285. .ip_period = ctx->b_per_p + 1,
  286. .picture_width = avctx->width,
  287. .picture_height = avctx->height,
  288. .bits_per_second = ctx->va_bit_rate,
  289. .frame_rate = av_q2d(priv->frame_rate),
  290. .aspect_ratio_information = sh->aspect_ratio_information,
  291. .vbv_buffer_size = priv->vbv_buffer_size,
  292. .sequence_extension.bits = {
  293. .profile_and_level_indication = se->profile_and_level_indication,
  294. .progressive_sequence = se->progressive_sequence,
  295. .chroma_format = se->chroma_format,
  296. .low_delay = se->low_delay,
  297. .frame_rate_extension_n = se->frame_rate_extension_n,
  298. .frame_rate_extension_d = se->frame_rate_extension_d,
  299. },
  300. .new_gop_header = 1,
  301. .gop_header.bits = {
  302. .time_code = goph->time_code,
  303. .closed_gop = goph->closed_gop,
  304. .broken_link = goph->broken_link,
  305. },
  306. };
  307. *vpic = (VAEncPictureParameterBufferMPEG2) {
  308. .forward_reference_picture = VA_INVALID_ID,
  309. .backward_reference_picture = VA_INVALID_ID,
  310. .reconstructed_picture = VA_INVALID_ID,
  311. .coded_buf = VA_INVALID_ID,
  312. .vbv_delay = 0xffff,
  313. .f_code = { { 15, 15 }, { 15, 15 } },
  314. .picture_coding_extension.bits = {
  315. .intra_dc_precision = pce->intra_dc_precision,
  316. .picture_structure = pce->picture_structure,
  317. .top_field_first = pce->top_field_first,
  318. .frame_pred_frame_dct = pce->frame_pred_frame_dct,
  319. .concealment_motion_vectors = pce->concealment_motion_vectors,
  320. .q_scale_type = pce->q_scale_type,
  321. .intra_vlc_format = pce->intra_vlc_format,
  322. .alternate_scan = pce->alternate_scan,
  323. .repeat_first_field = pce->repeat_first_field,
  324. .progressive_frame = pce->progressive_frame,
  325. .composite_display_flag = pce->composite_display_flag,
  326. },
  327. .composite_display.bits = {
  328. .v_axis = pce->v_axis,
  329. .field_sequence = pce->field_sequence,
  330. .sub_carrier = pce->sub_carrier,
  331. .burst_amplitude = pce->burst_amplitude,
  332. .sub_carrier_phase = pce->sub_carrier_phase,
  333. },
  334. };
  335. return 0;
  336. }
  337. static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
  338. VAAPIEncodePicture *pic)
  339. {
  340. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  341. MPEG2RawPictureHeader *ph = &priv->picture_header;
  342. MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
  343. VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params;
  344. if (pic->type == PICTURE_TYPE_IDR || pic->type == PICTURE_TYPE_I) {
  345. ph->temporal_reference = 0;
  346. ph->picture_coding_type = 1;
  347. priv->last_i_frame = pic->display_order;
  348. } else {
  349. ph->temporal_reference = pic->display_order - priv->last_i_frame;
  350. ph->picture_coding_type = pic->type == PICTURE_TYPE_B ? 3 : 2;
  351. }
  352. if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
  353. pce->f_code[0][0] = priv->f_code_horizontal;
  354. pce->f_code[0][1] = priv->f_code_vertical;
  355. } else {
  356. pce->f_code[0][0] = 15;
  357. pce->f_code[0][1] = 15;
  358. }
  359. if (pic->type == PICTURE_TYPE_B) {
  360. pce->f_code[1][0] = priv->f_code_horizontal;
  361. pce->f_code[1][1] = priv->f_code_vertical;
  362. } else {
  363. pce->f_code[1][0] = 15;
  364. pce->f_code[1][1] = 15;
  365. }
  366. vpic->reconstructed_picture = pic->recon_surface;
  367. vpic->coded_buf = pic->output_buffer;
  368. switch (pic->type) {
  369. case PICTURE_TYPE_IDR:
  370. case PICTURE_TYPE_I:
  371. vpic->picture_type = VAEncPictureTypeIntra;
  372. break;
  373. case PICTURE_TYPE_P:
  374. vpic->picture_type = VAEncPictureTypePredictive;
  375. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  376. break;
  377. case PICTURE_TYPE_B:
  378. vpic->picture_type = VAEncPictureTypeBidirectional;
  379. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  380. vpic->backward_reference_picture = pic->refs[1]->recon_surface;
  381. break;
  382. default:
  383. av_assert0(0 && "invalid picture type");
  384. }
  385. vpic->temporal_reference = ph->temporal_reference;
  386. vpic->f_code[0][0] = pce->f_code[0][0];
  387. vpic->f_code[0][1] = pce->f_code[0][1];
  388. vpic->f_code[1][0] = pce->f_code[1][0];
  389. vpic->f_code[1][1] = pce->f_code[1][1];
  390. pic->nb_slices = priv->mb_height;
  391. return 0;
  392. }
  393. static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
  394. VAAPIEncodePicture *pic,
  395. VAAPIEncodeSlice *slice)
  396. {
  397. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  398. VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
  399. int qp;
  400. vslice->macroblock_address = priv->mb_width * slice->index;
  401. vslice->num_macroblocks = priv->mb_width;
  402. switch (pic->type) {
  403. case PICTURE_TYPE_IDR:
  404. case PICTURE_TYPE_I:
  405. qp = priv->quant_i;
  406. break;
  407. case PICTURE_TYPE_P:
  408. qp = priv->quant_p;
  409. break;
  410. case PICTURE_TYPE_B:
  411. qp = priv->quant_b;
  412. break;
  413. default:
  414. av_assert0(0 && "invalid picture type");
  415. }
  416. vslice->quantiser_scale_code = qp;
  417. vslice->is_intra_slice = (pic->type == PICTURE_TYPE_IDR ||
  418. pic->type == PICTURE_TYPE_I);
  419. return 0;
  420. }
  421. static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
  422. {
  423. VAAPIEncodeContext *ctx = avctx->priv_data;
  424. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  425. int err;
  426. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
  427. if (err < 0)
  428. return err;
  429. priv->mb_width = FFALIGN(avctx->width, 16) / 16;
  430. priv->mb_height = FFALIGN(avctx->height, 16) / 16;
  431. if (ctx->va_rc_mode == VA_RC_CQP) {
  432. priv->quant_p = av_clip(avctx->global_quality, 1, 31);
  433. if (avctx->i_quant_factor > 0.0)
  434. priv->quant_i = av_clip((avctx->global_quality *
  435. avctx->i_quant_factor +
  436. avctx->i_quant_offset) + 0.5,
  437. 1, 31);
  438. else
  439. priv->quant_i = priv->quant_p;
  440. if (avctx->b_quant_factor > 0.0)
  441. priv->quant_b = av_clip((avctx->global_quality *
  442. avctx->b_quant_factor +
  443. avctx->b_quant_offset) + 0.5,
  444. 1, 31);
  445. else
  446. priv->quant_b = priv->quant_p;
  447. av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
  448. "%d / %d / %d for I- / P- / B-frames.\n",
  449. priv->quant_i, priv->quant_p, priv->quant_b);
  450. } else {
  451. av_assert0(0 && "Invalid RC mode.");
  452. }
  453. return 0;
  454. }
  455. static const VAAPIEncodeProfile vaapi_encode_mpeg2_profiles[] = {
  456. { FF_PROFILE_MPEG2_MAIN, 8, 3, 1, 1, VAProfileMPEG2Main },
  457. { FF_PROFILE_MPEG2_SIMPLE, 8, 3, 1, 1, VAProfileMPEG2Simple },
  458. { FF_PROFILE_UNKNOWN }
  459. };
  460. static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
  461. .profiles = vaapi_encode_mpeg2_profiles,
  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. .encode2 = &ff_vaapi_encode2,
  564. .close = &vaapi_encode_mpeg2_close,
  565. .priv_class = &vaapi_encode_mpeg2_class,
  566. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  567. .defaults = vaapi_encode_mpeg2_defaults,
  568. .pix_fmts = (const enum AVPixelFormat[]) {
  569. AV_PIX_FMT_VAAPI,
  570. AV_PIX_FMT_NONE,
  571. },
  572. .wrapper_name = "vaapi",
  573. };