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.

714 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_reset(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_reset(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. // Marker bit in the middle of time_code.
  256. goph->time_code = 1 << 12;
  257. goph->closed_gop = 1;
  258. goph->broken_link = 0;
  259. // Defaults for picture header
  260. ph->picture_start_code = MPEG2_START_PICTURE;
  261. ph->vbv_delay = 0xffff; // Not currently calculated.
  262. ph->full_pel_forward_vector = 0;
  263. ph->forward_f_code = 7;
  264. ph->full_pel_backward_vector = 0;
  265. ph->forward_f_code = 7;
  266. // Defaults for picture coding extension
  267. priv->picture_coding_extension.extension_start_code =
  268. MPEG2_START_EXTENSION;
  269. priv->picture_coding_extension.extension_start_code_identifier =
  270. MPEG2_EXTENSION_PICTURE_CODING;
  271. pce->intra_dc_precision = 0;
  272. pce->picture_structure = 3;
  273. pce->top_field_first = 0;
  274. pce->frame_pred_frame_dct = 1;
  275. pce->concealment_motion_vectors = 0;
  276. pce->q_scale_type = 0;
  277. pce->intra_vlc_format = 0;
  278. pce->alternate_scan = 0;
  279. pce->repeat_first_field = 0;
  280. pce->progressive_frame = 1;
  281. pce->composite_display_flag = 0;
  282. *vseq = (VAEncSequenceParameterBufferMPEG2) {
  283. .intra_period = ctx->gop_size,
  284. .ip_period = ctx->b_per_p + 1,
  285. .picture_width = avctx->width,
  286. .picture_height = avctx->height,
  287. .bits_per_second = ctx->va_bit_rate,
  288. .frame_rate = av_q2d(priv->frame_rate),
  289. .aspect_ratio_information = sh->aspect_ratio_information,
  290. .vbv_buffer_size = priv->vbv_buffer_size,
  291. .sequence_extension.bits = {
  292. .profile_and_level_indication = se->profile_and_level_indication,
  293. .progressive_sequence = se->progressive_sequence,
  294. .chroma_format = se->chroma_format,
  295. .low_delay = se->low_delay,
  296. .frame_rate_extension_n = se->frame_rate_extension_n,
  297. .frame_rate_extension_d = se->frame_rate_extension_d,
  298. },
  299. .new_gop_header = 1,
  300. .gop_header.bits = {
  301. .time_code = goph->time_code,
  302. .closed_gop = goph->closed_gop,
  303. .broken_link = goph->broken_link,
  304. },
  305. };
  306. *vpic = (VAEncPictureParameterBufferMPEG2) {
  307. .forward_reference_picture = VA_INVALID_ID,
  308. .backward_reference_picture = VA_INVALID_ID,
  309. .reconstructed_picture = VA_INVALID_ID,
  310. .coded_buf = VA_INVALID_ID,
  311. .vbv_delay = 0xffff,
  312. .f_code = { { 15, 15 }, { 15, 15 } },
  313. .picture_coding_extension.bits = {
  314. .intra_dc_precision = pce->intra_dc_precision,
  315. .picture_structure = pce->picture_structure,
  316. .top_field_first = pce->top_field_first,
  317. .frame_pred_frame_dct = pce->frame_pred_frame_dct,
  318. .concealment_motion_vectors = pce->concealment_motion_vectors,
  319. .q_scale_type = pce->q_scale_type,
  320. .intra_vlc_format = pce->intra_vlc_format,
  321. .alternate_scan = pce->alternate_scan,
  322. .repeat_first_field = pce->repeat_first_field,
  323. .progressive_frame = pce->progressive_frame,
  324. .composite_display_flag = pce->composite_display_flag,
  325. },
  326. .composite_display.bits = {
  327. .v_axis = pce->v_axis,
  328. .field_sequence = pce->field_sequence,
  329. .sub_carrier = pce->sub_carrier,
  330. .burst_amplitude = pce->burst_amplitude,
  331. .sub_carrier_phase = pce->sub_carrier_phase,
  332. },
  333. };
  334. return 0;
  335. }
  336. static int vaapi_encode_mpeg2_init_picture_params(AVCodecContext *avctx,
  337. VAAPIEncodePicture *pic)
  338. {
  339. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  340. MPEG2RawPictureHeader *ph = &priv->picture_header;
  341. MPEG2RawPictureCodingExtension *pce = &priv->picture_coding_extension.data.picture_coding;
  342. VAEncPictureParameterBufferMPEG2 *vpic = pic->codec_picture_params;
  343. if (pic->type == PICTURE_TYPE_IDR || pic->type == PICTURE_TYPE_I) {
  344. ph->temporal_reference = 0;
  345. ph->picture_coding_type = 1;
  346. priv->last_i_frame = pic->display_order;
  347. } else {
  348. ph->temporal_reference = pic->display_order - priv->last_i_frame;
  349. ph->picture_coding_type = pic->type == PICTURE_TYPE_B ? 3 : 2;
  350. }
  351. if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
  352. pce->f_code[0][0] = priv->f_code_horizontal;
  353. pce->f_code[0][1] = priv->f_code_vertical;
  354. } else {
  355. pce->f_code[0][0] = 15;
  356. pce->f_code[0][1] = 15;
  357. }
  358. if (pic->type == PICTURE_TYPE_B) {
  359. pce->f_code[1][0] = priv->f_code_horizontal;
  360. pce->f_code[1][1] = priv->f_code_vertical;
  361. } else {
  362. pce->f_code[1][0] = 15;
  363. pce->f_code[1][1] = 15;
  364. }
  365. vpic->reconstructed_picture = pic->recon_surface;
  366. vpic->coded_buf = pic->output_buffer;
  367. switch (pic->type) {
  368. case PICTURE_TYPE_IDR:
  369. case PICTURE_TYPE_I:
  370. vpic->picture_type = VAEncPictureTypeIntra;
  371. break;
  372. case PICTURE_TYPE_P:
  373. vpic->picture_type = VAEncPictureTypePredictive;
  374. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  375. break;
  376. case PICTURE_TYPE_B:
  377. vpic->picture_type = VAEncPictureTypeBidirectional;
  378. vpic->forward_reference_picture = pic->refs[0]->recon_surface;
  379. vpic->backward_reference_picture = pic->refs[1]->recon_surface;
  380. break;
  381. default:
  382. av_assert0(0 && "invalid picture type");
  383. }
  384. vpic->temporal_reference = ph->temporal_reference;
  385. vpic->f_code[0][0] = pce->f_code[0][0];
  386. vpic->f_code[0][1] = pce->f_code[0][1];
  387. vpic->f_code[1][0] = pce->f_code[1][0];
  388. vpic->f_code[1][1] = pce->f_code[1][1];
  389. return 0;
  390. }
  391. static int vaapi_encode_mpeg2_init_slice_params(AVCodecContext *avctx,
  392. VAAPIEncodePicture *pic,
  393. VAAPIEncodeSlice *slice)
  394. {
  395. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  396. VAEncSliceParameterBufferMPEG2 *vslice = slice->codec_slice_params;
  397. int qp;
  398. vslice->macroblock_address = slice->block_start;
  399. vslice->num_macroblocks = slice->block_size;
  400. switch (pic->type) {
  401. case PICTURE_TYPE_IDR:
  402. case PICTURE_TYPE_I:
  403. qp = priv->quant_i;
  404. break;
  405. case PICTURE_TYPE_P:
  406. qp = priv->quant_p;
  407. break;
  408. case PICTURE_TYPE_B:
  409. qp = priv->quant_b;
  410. break;
  411. default:
  412. av_assert0(0 && "invalid picture type");
  413. }
  414. vslice->quantiser_scale_code = qp;
  415. vslice->is_intra_slice = (pic->type == PICTURE_TYPE_IDR ||
  416. pic->type == PICTURE_TYPE_I);
  417. return 0;
  418. }
  419. static av_cold int vaapi_encode_mpeg2_configure(AVCodecContext *avctx)
  420. {
  421. VAAPIEncodeContext *ctx = avctx->priv_data;
  422. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  423. int err;
  424. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MPEG2VIDEO, avctx);
  425. if (err < 0)
  426. return err;
  427. if (ctx->va_rc_mode == VA_RC_CQP) {
  428. priv->quant_p = av_clip(ctx->rc_quality, 1, 31);
  429. if (avctx->i_quant_factor > 0.0)
  430. priv->quant_i =
  431. av_clip((avctx->i_quant_factor * priv->quant_p +
  432. avctx->i_quant_offset) + 0.5, 1, 31);
  433. else
  434. priv->quant_i = priv->quant_p;
  435. if (avctx->b_quant_factor > 0.0)
  436. priv->quant_b =
  437. av_clip((avctx->b_quant_factor * priv->quant_p +
  438. avctx->b_quant_offset) + 0.5, 1, 31);
  439. else
  440. priv->quant_b = priv->quant_p;
  441. av_log(avctx, AV_LOG_DEBUG, "Using fixed quantiser "
  442. "%d / %d / %d for I- / P- / B-frames.\n",
  443. priv->quant_i, priv->quant_p, priv->quant_b);
  444. } else {
  445. priv->quant_i = 16;
  446. priv->quant_p = 16;
  447. priv->quant_b = 16;
  448. }
  449. ctx->slice_block_rows = FFALIGN(avctx->height, 16) / 16;
  450. ctx->slice_block_cols = FFALIGN(avctx->width, 16) / 16;
  451. ctx->nb_slices = ctx->slice_block_rows;
  452. ctx->slice_size = 1;
  453. ctx->roi_quant_range = 31;
  454. return 0;
  455. }
  456. static const VAAPIEncodeProfile vaapi_encode_mpeg2_profiles[] = {
  457. { FF_PROFILE_MPEG2_MAIN, 8, 3, 1, 1, VAProfileMPEG2Main },
  458. { FF_PROFILE_MPEG2_SIMPLE, 8, 3, 1, 1, VAProfileMPEG2Simple },
  459. { FF_PROFILE_UNKNOWN }
  460. };
  461. static const VAAPIEncodeType vaapi_encode_type_mpeg2 = {
  462. .profiles = vaapi_encode_mpeg2_profiles,
  463. .flags = FLAG_B_PICTURES,
  464. .configure = &vaapi_encode_mpeg2_configure,
  465. .default_quality = 10,
  466. .sequence_params_size = sizeof(VAEncSequenceParameterBufferMPEG2),
  467. .init_sequence_params = &vaapi_encode_mpeg2_init_sequence_params,
  468. .picture_params_size = sizeof(VAEncPictureParameterBufferMPEG2),
  469. .init_picture_params = &vaapi_encode_mpeg2_init_picture_params,
  470. .slice_params_size = sizeof(VAEncSliceParameterBufferMPEG2),
  471. .init_slice_params = &vaapi_encode_mpeg2_init_slice_params,
  472. .sequence_header_type = VAEncPackedHeaderSequence,
  473. .write_sequence_header = &vaapi_encode_mpeg2_write_sequence_header,
  474. .picture_header_type = VAEncPackedHeaderPicture,
  475. .write_picture_header = &vaapi_encode_mpeg2_write_picture_header,
  476. };
  477. static av_cold int vaapi_encode_mpeg2_init(AVCodecContext *avctx)
  478. {
  479. VAAPIEncodeContext *ctx = avctx->priv_data;
  480. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  481. ctx->codec = &vaapi_encode_type_mpeg2;
  482. if (avctx->profile == FF_PROFILE_UNKNOWN)
  483. avctx->profile = priv->profile;
  484. if (avctx->level == FF_LEVEL_UNKNOWN)
  485. avctx->level = priv->level;
  486. // Reject unknown levels (these are required to set f_code for
  487. // motion vector encoding).
  488. switch (avctx->level) {
  489. case 4: // High
  490. case 6: // High 1440
  491. case 8: // Main
  492. case 10: // Low
  493. break;
  494. default:
  495. av_log(avctx, AV_LOG_ERROR, "Unknown MPEG-2 level %d.\n",
  496. avctx->level);
  497. return AVERROR(EINVAL);
  498. }
  499. if (avctx->height % 4096 == 0 || avctx->width % 4096 == 0) {
  500. av_log(avctx, AV_LOG_ERROR, "MPEG-2 does not support picture "
  501. "height or width divisible by 4096.\n");
  502. return AVERROR(EINVAL);
  503. }
  504. ctx->desired_packed_headers = VA_ENC_PACKED_HEADER_SEQUENCE |
  505. VA_ENC_PACKED_HEADER_PICTURE;
  506. ctx->surface_width = FFALIGN(avctx->width, 16);
  507. ctx->surface_height = FFALIGN(avctx->height, 16);
  508. return ff_vaapi_encode_init(avctx);
  509. }
  510. static av_cold int vaapi_encode_mpeg2_close(AVCodecContext *avctx)
  511. {
  512. VAAPIEncodeMPEG2Context *priv = avctx->priv_data;
  513. ff_cbs_fragment_free(priv->cbc, &priv->current_fragment);
  514. ff_cbs_close(&priv->cbc);
  515. return ff_vaapi_encode_close(avctx);
  516. }
  517. #define OFFSET(x) offsetof(VAAPIEncodeMPEG2Context, x)
  518. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  519. static const AVOption vaapi_encode_mpeg2_options[] = {
  520. VAAPI_ENCODE_COMMON_OPTIONS,
  521. VAAPI_ENCODE_RC_OPTIONS,
  522. { "profile", "Set profile (in profile_and_level_indication)",
  523. OFFSET(profile), AV_OPT_TYPE_INT,
  524. { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 7, FLAGS, "profile" },
  525. #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  526. { .i64 = value }, 0, 0, FLAGS, "profile"
  527. { PROFILE("simple", FF_PROFILE_MPEG2_SIMPLE) },
  528. { PROFILE("main", FF_PROFILE_MPEG2_MAIN) },
  529. #undef PROFILE
  530. { "level", "Set level (in profile_and_level_indication)",
  531. OFFSET(level), AV_OPT_TYPE_INT,
  532. { .i64 = 4 }, 0, 15, FLAGS, "level" },
  533. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  534. { .i64 = value }, 0, 0, FLAGS, "level"
  535. { LEVEL("low", 10) },
  536. { LEVEL("main", 8) },
  537. { LEVEL("high_1440", 6) },
  538. { LEVEL("high", 4) },
  539. #undef LEVEL
  540. { NULL },
  541. };
  542. static const AVCodecDefault vaapi_encode_mpeg2_defaults[] = {
  543. { "b", "0" },
  544. { "bf", "1" },
  545. { "g", "120" },
  546. { "i_qfactor", "1" },
  547. { "i_qoffset", "0" },
  548. { "b_qfactor", "6/5" },
  549. { "b_qoffset", "0" },
  550. { "qmin", "-1" },
  551. { "qmax", "-1" },
  552. { NULL },
  553. };
  554. static const AVClass vaapi_encode_mpeg2_class = {
  555. .class_name = "mpeg2_vaapi",
  556. .item_name = av_default_item_name,
  557. .option = vaapi_encode_mpeg2_options,
  558. .version = LIBAVUTIL_VERSION_INT,
  559. };
  560. AVCodec ff_mpeg2_vaapi_encoder = {
  561. .name = "mpeg2_vaapi",
  562. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 (VAAPI)"),
  563. .type = AVMEDIA_TYPE_VIDEO,
  564. .id = AV_CODEC_ID_MPEG2VIDEO,
  565. .priv_data_size = sizeof(VAAPIEncodeMPEG2Context),
  566. .init = &vaapi_encode_mpeg2_init,
  567. .send_frame = &ff_vaapi_encode_send_frame,
  568. .receive_packet = &ff_vaapi_encode_receive_packet,
  569. .close = &vaapi_encode_mpeg2_close,
  570. .priv_class = &vaapi_encode_mpeg2_class,
  571. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  572. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  573. .defaults = vaapi_encode_mpeg2_defaults,
  574. .pix_fmts = (const enum AVPixelFormat[]) {
  575. AV_PIX_FMT_VAAPI,
  576. AV_PIX_FMT_NONE,
  577. },
  578. .hw_configs = ff_vaapi_encode_hw_configs,
  579. .wrapper_name = "vaapi",
  580. };