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.

1111 lines
35KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <inttypes.h>
  19. #include <string.h>
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/log.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "vaapi_encode.h"
  24. #include "avcodec.h"
  25. static const char *picture_type_name[] = { "IDR", "I", "P", "B" };
  26. static int vaapi_encode_make_packed_header(AVCodecContext *avctx,
  27. VAAPIEncodePicture *pic,
  28. int type, char *data, size_t bit_len)
  29. {
  30. VAAPIEncodeContext *ctx = avctx->priv_data;
  31. VAStatus vas;
  32. VABufferID param_buffer, data_buffer;
  33. VAEncPackedHeaderParameterBuffer params = {
  34. .type = type,
  35. .bit_length = bit_len,
  36. .has_emulation_bytes = 1,
  37. };
  38. av_assert0(pic->nb_param_buffers + 2 <= MAX_PARAM_BUFFERS);
  39. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  40. VAEncPackedHeaderParameterBufferType,
  41. sizeof(params), 1, &params, &param_buffer);
  42. if (vas != VA_STATUS_SUCCESS) {
  43. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
  44. "for packed header (type %d): %d (%s).\n",
  45. type, vas, vaErrorStr(vas));
  46. return AVERROR(EIO);
  47. }
  48. pic->param_buffers[pic->nb_param_buffers++] = param_buffer;
  49. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  50. VAEncPackedHeaderDataBufferType,
  51. (bit_len + 7) / 8, 1, data, &data_buffer);
  52. if (vas != VA_STATUS_SUCCESS) {
  53. av_log(avctx, AV_LOG_ERROR, "Failed to create data buffer "
  54. "for packed header (type %d): %d (%s).\n",
  55. type, vas, vaErrorStr(vas));
  56. return AVERROR(EIO);
  57. }
  58. pic->param_buffers[pic->nb_param_buffers++] = data_buffer;
  59. av_log(avctx, AV_LOG_DEBUG, "Packed header buffer (%d) is %#x/%#x "
  60. "(%zu bits).\n", type, param_buffer, data_buffer, bit_len);
  61. return 0;
  62. }
  63. static int vaapi_encode_make_param_buffer(AVCodecContext *avctx,
  64. VAAPIEncodePicture *pic,
  65. int type, char *data, size_t len)
  66. {
  67. VAAPIEncodeContext *ctx = avctx->priv_data;
  68. VAStatus vas;
  69. VABufferID buffer;
  70. av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS);
  71. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  72. type, len, 1, data, &buffer);
  73. if (vas != VA_STATUS_SUCCESS) {
  74. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
  75. "(type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
  76. return AVERROR(EIO);
  77. }
  78. pic->param_buffers[pic->nb_param_buffers++] = buffer;
  79. av_log(avctx, AV_LOG_DEBUG, "Param buffer (%d) is %#x.\n",
  80. type, buffer);
  81. return 0;
  82. }
  83. static int vaapi_encode_wait(AVCodecContext *avctx,
  84. VAAPIEncodePicture *pic)
  85. {
  86. VAAPIEncodeContext *ctx = avctx->priv_data;
  87. VAStatus vas;
  88. av_assert0(pic->encode_issued);
  89. if (pic->encode_complete) {
  90. // Already waited for this picture.
  91. return 0;
  92. }
  93. av_log(avctx, AV_LOG_DEBUG, "Sync to pic %"PRId64"/%"PRId64" "
  94. "(recon surface %#x).\n", pic->display_order,
  95. pic->encode_order, pic->recon_surface);
  96. vas = vaSyncSurface(ctx->hwctx->display, pic->recon_surface);
  97. if (vas != VA_STATUS_SUCCESS) {
  98. av_log(avctx, AV_LOG_ERROR, "Failed to sync to picture completion: "
  99. "%d (%s).\n", vas, vaErrorStr(vas));
  100. return AVERROR(EIO);
  101. }
  102. // Input is definitely finished with now.
  103. av_frame_free(&pic->input_image);
  104. pic->encode_complete = 1;
  105. return 0;
  106. }
  107. static int vaapi_encode_issue(AVCodecContext *avctx,
  108. VAAPIEncodePicture *pic)
  109. {
  110. VAAPIEncodeContext *ctx = avctx->priv_data;
  111. VAAPIEncodeSlice *slice;
  112. VAStatus vas;
  113. int err, i;
  114. char data[MAX_PARAM_BUFFER_SIZE];
  115. size_t bit_len;
  116. av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
  117. "as type %s.\n", pic->display_order, pic->encode_order,
  118. picture_type_name[pic->type]);
  119. if (pic->nb_refs == 0) {
  120. av_log(avctx, AV_LOG_DEBUG, "No reference pictures.\n");
  121. } else {
  122. av_log(avctx, AV_LOG_DEBUG, "Refers to:");
  123. for (i = 0; i < pic->nb_refs; i++) {
  124. av_log(avctx, AV_LOG_DEBUG, " %"PRId64"/%"PRId64,
  125. pic->refs[i]->display_order, pic->refs[i]->encode_order);
  126. }
  127. av_log(avctx, AV_LOG_DEBUG, ".\n");
  128. }
  129. av_assert0(pic->input_available && !pic->encode_issued);
  130. for (i = 0; i < pic->nb_refs; i++) {
  131. av_assert0(pic->refs[i]);
  132. // If we are serialised then the references must have already
  133. // completed. If not, they must have been issued but need not
  134. // have completed yet.
  135. if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
  136. av_assert0(pic->refs[i]->encode_complete);
  137. else
  138. av_assert0(pic->refs[i]->encode_issued);
  139. }
  140. av_log(avctx, AV_LOG_DEBUG, "Input surface is %#x.\n", pic->input_surface);
  141. pic->recon_image = av_frame_alloc();
  142. if (!pic->recon_image) {
  143. err = AVERROR(ENOMEM);
  144. goto fail;
  145. }
  146. err = av_hwframe_get_buffer(ctx->recon_frames_ref, pic->recon_image, 0);
  147. if (err < 0) {
  148. err = AVERROR(ENOMEM);
  149. goto fail;
  150. }
  151. pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
  152. av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
  153. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  154. VAEncCodedBufferType,
  155. MAX_OUTPUT_BUFFER_SIZE, 1, 0,
  156. &pic->output_buffer);
  157. if (vas != VA_STATUS_SUCCESS) {
  158. av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
  159. "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
  160. err = AVERROR(ENOMEM);
  161. goto fail;
  162. }
  163. av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",
  164. pic->output_buffer);
  165. if (ctx->codec->picture_params_size > 0) {
  166. pic->codec_picture_params = av_malloc(ctx->codec->picture_params_size);
  167. if (!pic->codec_picture_params)
  168. goto fail;
  169. memcpy(pic->codec_picture_params, ctx->codec_picture_params,
  170. ctx->codec->picture_params_size);
  171. } else {
  172. av_assert0(!ctx->codec_picture_params);
  173. }
  174. pic->nb_param_buffers = 0;
  175. if (pic->encode_order == 0) {
  176. // Global parameter buffers are set on the first picture only.
  177. for (i = 0; i < ctx->nb_global_params; i++) {
  178. err = vaapi_encode_make_param_buffer(avctx, pic,
  179. VAEncMiscParameterBufferType,
  180. (char*)ctx->global_params[i],
  181. ctx->global_params_size[i]);
  182. if (err < 0)
  183. goto fail;
  184. }
  185. }
  186. if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
  187. err = vaapi_encode_make_param_buffer(avctx, pic,
  188. VAEncSequenceParameterBufferType,
  189. ctx->codec_sequence_params,
  190. ctx->codec->sequence_params_size);
  191. if (err < 0)
  192. goto fail;
  193. }
  194. if (ctx->codec->init_picture_params) {
  195. err = ctx->codec->init_picture_params(avctx, pic);
  196. if (err < 0) {
  197. av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
  198. "parameters: %d.\n", err);
  199. goto fail;
  200. }
  201. err = vaapi_encode_make_param_buffer(avctx, pic,
  202. VAEncPictureParameterBufferType,
  203. pic->codec_picture_params,
  204. ctx->codec->picture_params_size);
  205. if (err < 0)
  206. goto fail;
  207. }
  208. if (pic->type == PICTURE_TYPE_IDR) {
  209. if (ctx->codec->write_sequence_header) {
  210. bit_len = 8 * sizeof(data);
  211. err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
  212. if (err < 0) {
  213. av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
  214. "header: %d.\n", err);
  215. goto fail;
  216. }
  217. err = vaapi_encode_make_packed_header(avctx, pic,
  218. ctx->codec->sequence_header_type,
  219. data, bit_len);
  220. if (err < 0)
  221. goto fail;
  222. }
  223. }
  224. if (ctx->codec->write_picture_header) {
  225. bit_len = 8 * sizeof(data);
  226. err = ctx->codec->write_picture_header(avctx, pic, data, &bit_len);
  227. if (err < 0) {
  228. av_log(avctx, AV_LOG_ERROR, "Failed to write per-picture "
  229. "header: %d.\n", err);
  230. goto fail;
  231. }
  232. err = vaapi_encode_make_packed_header(avctx, pic,
  233. ctx->codec->picture_header_type,
  234. data, bit_len);
  235. if (err < 0)
  236. goto fail;
  237. }
  238. if (ctx->codec->write_extra_buffer) {
  239. for (i = 0;; i++) {
  240. size_t len = sizeof(data);
  241. int type;
  242. err = ctx->codec->write_extra_buffer(avctx, pic, i, &type,
  243. data, &len);
  244. if (err == AVERROR_EOF)
  245. break;
  246. if (err < 0) {
  247. av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
  248. "buffer %d: %d.\n", i, err);
  249. goto fail;
  250. }
  251. err = vaapi_encode_make_param_buffer(avctx, pic, type,
  252. data, len);
  253. if (err < 0)
  254. goto fail;
  255. }
  256. }
  257. av_assert0(pic->nb_slices <= MAX_PICTURE_SLICES);
  258. for (i = 0; i < pic->nb_slices; i++) {
  259. slice = av_mallocz(sizeof(*slice));
  260. if (!slice) {
  261. err = AVERROR(ENOMEM);
  262. goto fail;
  263. }
  264. pic->slices[i] = slice;
  265. if (ctx->codec->slice_params_size > 0) {
  266. slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
  267. if (!slice->codec_slice_params) {
  268. err = AVERROR(ENOMEM);
  269. goto fail;
  270. }
  271. }
  272. if (ctx->codec->init_slice_params) {
  273. err = ctx->codec->init_slice_params(avctx, pic, slice);
  274. if (err < 0) {
  275. av_log(avctx, AV_LOG_ERROR, "Failed to initalise slice "
  276. "parameters: %d.\n", err);
  277. goto fail;
  278. }
  279. }
  280. if (ctx->codec->write_slice_header) {
  281. bit_len = 8 * sizeof(data);
  282. err = ctx->codec->write_slice_header(avctx, pic, slice,
  283. data, &bit_len);
  284. if (err < 0) {
  285. av_log(avctx, AV_LOG_ERROR, "Failed to write per-slice "
  286. "header: %d.\n", err);
  287. goto fail;
  288. }
  289. err = vaapi_encode_make_packed_header(avctx, pic,
  290. ctx->codec->slice_header_type,
  291. data, bit_len);
  292. if (err < 0)
  293. goto fail;
  294. }
  295. if (ctx->codec->init_slice_params) {
  296. err = vaapi_encode_make_param_buffer(avctx, pic,
  297. VAEncSliceParameterBufferType,
  298. slice->codec_slice_params,
  299. ctx->codec->slice_params_size);
  300. if (err < 0)
  301. goto fail;
  302. }
  303. }
  304. vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
  305. pic->input_surface);
  306. if (vas != VA_STATUS_SUCCESS) {
  307. av_log(avctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
  308. "%d (%s).\n", vas, vaErrorStr(vas));
  309. err = AVERROR(EIO);
  310. goto fail_with_picture;
  311. }
  312. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  313. pic->param_buffers, pic->nb_param_buffers);
  314. if (vas != VA_STATUS_SUCCESS) {
  315. av_log(avctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
  316. "%d (%s).\n", vas, vaErrorStr(vas));
  317. err = AVERROR(EIO);
  318. goto fail_with_picture;
  319. }
  320. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  321. if (vas != VA_STATUS_SUCCESS) {
  322. av_log(avctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
  323. "%d (%s).\n", vas, vaErrorStr(vas));
  324. err = AVERROR(EIO);
  325. goto fail_at_end;
  326. }
  327. pic->encode_issued = 1;
  328. if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
  329. return vaapi_encode_wait(avctx, pic);
  330. else
  331. return 0;
  332. fail_with_picture:
  333. vaEndPicture(ctx->hwctx->display, ctx->va_context);
  334. fail:
  335. for(i = 0; i < pic->nb_param_buffers; i++)
  336. vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
  337. fail_at_end:
  338. av_freep(&pic->codec_picture_params);
  339. av_frame_free(&pic->recon_image);
  340. return err;
  341. }
  342. static int vaapi_encode_output(AVCodecContext *avctx,
  343. VAAPIEncodePicture *pic, AVPacket *pkt)
  344. {
  345. VAAPIEncodeContext *ctx = avctx->priv_data;
  346. VACodedBufferSegment *buf_list, *buf;
  347. VAStatus vas;
  348. int err;
  349. err = vaapi_encode_wait(avctx, pic);
  350. if (err < 0)
  351. return err;
  352. buf_list = NULL;
  353. vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
  354. (void**)&buf_list);
  355. if (vas != VA_STATUS_SUCCESS) {
  356. av_log(avctx, AV_LOG_ERROR, "Failed to map output buffers: "
  357. "%d (%s).\n", vas, vaErrorStr(vas));
  358. err = AVERROR(EIO);
  359. goto fail;
  360. }
  361. for (buf = buf_list; buf; buf = buf->next) {
  362. av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
  363. "(status %08x).\n", buf->size, buf->status);
  364. err = av_new_packet(pkt, buf->size);
  365. if (err < 0)
  366. goto fail;
  367. memcpy(pkt->data, buf->buf, buf->size);
  368. }
  369. if (pic->type == PICTURE_TYPE_IDR)
  370. pkt->flags |= AV_PKT_FLAG_KEY;
  371. pkt->pts = pic->pts;
  372. vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
  373. if (vas != VA_STATUS_SUCCESS) {
  374. av_log(avctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
  375. "%d (%s).\n", vas, vaErrorStr(vas));
  376. err = AVERROR(EIO);
  377. goto fail;
  378. }
  379. vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
  380. pic->output_buffer = VA_INVALID_ID;
  381. av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
  382. pic->display_order, pic->encode_order);
  383. return 0;
  384. fail:
  385. if (pic->output_buffer != VA_INVALID_ID) {
  386. vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
  387. vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
  388. pic->output_buffer = VA_INVALID_ID;
  389. }
  390. return err;
  391. }
  392. static int vaapi_encode_discard(AVCodecContext *avctx,
  393. VAAPIEncodePicture *pic)
  394. {
  395. VAAPIEncodeContext *ctx = avctx->priv_data;
  396. vaapi_encode_wait(avctx, pic);
  397. if (pic->output_buffer != VA_INVALID_ID) {
  398. av_log(avctx, AV_LOG_DEBUG, "Discard output for pic "
  399. "%"PRId64"/%"PRId64".\n",
  400. pic->display_order, pic->encode_order);
  401. vaDestroyBuffer(ctx->hwctx->display, pic->output_buffer);
  402. pic->output_buffer = VA_INVALID_ID;
  403. }
  404. return 0;
  405. }
  406. static VAAPIEncodePicture *vaapi_encode_alloc(void)
  407. {
  408. VAAPIEncodePicture *pic;
  409. pic = av_mallocz(sizeof(*pic));
  410. if (!pic)
  411. return NULL;
  412. pic->input_surface = VA_INVALID_ID;
  413. pic->recon_surface = VA_INVALID_ID;
  414. pic->output_buffer = VA_INVALID_ID;
  415. return pic;
  416. }
  417. static int vaapi_encode_free(AVCodecContext *avctx,
  418. VAAPIEncodePicture *pic)
  419. {
  420. int i;
  421. if (pic->encode_issued)
  422. vaapi_encode_discard(avctx, pic);
  423. for (i = 0; i < pic->nb_slices; i++) {
  424. av_freep(&pic->slices[i]->priv_data);
  425. av_freep(&pic->slices[i]->codec_slice_params);
  426. av_freep(&pic->slices[i]);
  427. }
  428. av_freep(&pic->codec_picture_params);
  429. av_frame_free(&pic->input_image);
  430. av_frame_free(&pic->recon_image);
  431. // Output buffer should already be destroyed.
  432. av_assert0(pic->output_buffer == VA_INVALID_ID);
  433. av_freep(&pic->priv_data);
  434. av_freep(&pic->codec_picture_params);
  435. av_free(pic);
  436. return 0;
  437. }
  438. static int vaapi_encode_step(AVCodecContext *avctx,
  439. VAAPIEncodePicture *target)
  440. {
  441. VAAPIEncodeContext *ctx = avctx->priv_data;
  442. VAAPIEncodePicture *pic;
  443. int i, err;
  444. if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING ||
  445. ctx->issue_mode == ISSUE_MODE_MINIMISE_LATENCY) {
  446. // These two modes are equivalent, except that we wait for
  447. // immediate completion on each operation if serialised.
  448. if (!target) {
  449. // No target, nothing to do yet.
  450. return 0;
  451. }
  452. if (target->encode_complete) {
  453. // Already done.
  454. return 0;
  455. }
  456. pic = target;
  457. for (i = 0; i < pic->nb_refs; i++) {
  458. if (!pic->refs[i]->encode_complete) {
  459. err = vaapi_encode_step(avctx, pic->refs[i]);
  460. if (err < 0)
  461. return err;
  462. }
  463. }
  464. err = vaapi_encode_issue(avctx, pic);
  465. if (err < 0)
  466. return err;
  467. } else if (ctx->issue_mode == ISSUE_MODE_MAXIMISE_THROUGHPUT) {
  468. int activity;
  469. do {
  470. activity = 0;
  471. for (pic = ctx->pic_start; pic; pic = pic->next) {
  472. if (!pic->input_available || pic->encode_issued)
  473. continue;
  474. for (i = 0; i < pic->nb_refs; i++) {
  475. if (!pic->refs[i]->encode_issued)
  476. break;
  477. }
  478. if (i < pic->nb_refs)
  479. continue;
  480. err = vaapi_encode_issue(avctx, pic);
  481. if (err < 0)
  482. return err;
  483. activity = 1;
  484. }
  485. } while(activity);
  486. if (target) {
  487. av_assert0(target->encode_issued && "broken dependencies?");
  488. }
  489. } else {
  490. av_assert0(0);
  491. }
  492. return 0;
  493. }
  494. static int vaapi_encode_get_next(AVCodecContext *avctx,
  495. VAAPIEncodePicture **pic_out)
  496. {
  497. VAAPIEncodeContext *ctx = avctx->priv_data;
  498. VAAPIEncodePicture *start, *end, *pic;
  499. int i;
  500. for (pic = ctx->pic_start; pic; pic = pic->next) {
  501. if (pic->next)
  502. av_assert0(pic->display_order + 1 == pic->next->display_order);
  503. if (pic->display_order == ctx->input_order) {
  504. *pic_out = pic;
  505. return 0;
  506. }
  507. }
  508. if (ctx->input_order == 0) {
  509. // First frame is always an IDR frame.
  510. av_assert0(!ctx->pic_start && !ctx->pic_end);
  511. pic = vaapi_encode_alloc();
  512. if (!pic)
  513. return AVERROR(ENOMEM);
  514. pic->type = PICTURE_TYPE_IDR;
  515. pic->display_order = 0;
  516. pic->encode_order = 0;
  517. ctx->pic_start = ctx->pic_end = pic;
  518. *pic_out = pic;
  519. return 0;
  520. }
  521. pic = vaapi_encode_alloc();
  522. if (!pic)
  523. return AVERROR(ENOMEM);
  524. if (ctx->p_per_i == 0 || ctx->p_counter == ctx->p_per_i) {
  525. if (ctx->i_per_idr == 0 || ctx->i_counter == ctx->i_per_idr) {
  526. pic->type = PICTURE_TYPE_IDR;
  527. ctx->i_counter = 0;
  528. } else {
  529. pic->type = PICTURE_TYPE_I;
  530. ++ctx->i_counter;
  531. }
  532. ctx->p_counter = 0;
  533. } else {
  534. pic->type = PICTURE_TYPE_P;
  535. pic->refs[0] = ctx->pic_end;
  536. pic->nb_refs = 1;
  537. ++ctx->p_counter;
  538. }
  539. start = end = pic;
  540. if (pic->type != PICTURE_TYPE_IDR) {
  541. // If that was not an IDR frame, add B-frames display-before and
  542. // encode-after it.
  543. for (i = 0; i < ctx->b_per_p; i++) {
  544. pic = vaapi_encode_alloc();
  545. if (!pic)
  546. goto fail;
  547. pic->type = PICTURE_TYPE_B;
  548. pic->refs[0] = ctx->pic_end;
  549. pic->refs[1] = end;
  550. pic->nb_refs = 2;
  551. pic->next = start;
  552. pic->display_order = ctx->input_order + ctx->b_per_p - i - 1;
  553. pic->encode_order = pic->display_order + 1;
  554. start = pic;
  555. }
  556. }
  557. for (i = 0, pic = start; pic; i++, pic = pic->next) {
  558. pic->display_order = ctx->input_order + i;
  559. if (end->type == PICTURE_TYPE_IDR)
  560. pic->encode_order = ctx->input_order + i;
  561. else if (pic == end)
  562. pic->encode_order = ctx->input_order;
  563. else
  564. pic->encode_order = ctx->input_order + i + 1;
  565. }
  566. av_assert0(ctx->pic_end);
  567. ctx->pic_end->next = start;
  568. ctx->pic_end = end;
  569. *pic_out = start;
  570. av_log(avctx, AV_LOG_DEBUG, "Pictures:");
  571. for (pic = ctx->pic_start; pic; pic = pic->next) {
  572. av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
  573. picture_type_name[pic->type],
  574. pic->display_order, pic->encode_order);
  575. }
  576. av_log(avctx, AV_LOG_DEBUG, "\n");
  577. return 0;
  578. fail:
  579. while (start) {
  580. pic = start->next;
  581. vaapi_encode_free(avctx, start);
  582. start = pic;
  583. }
  584. return AVERROR(ENOMEM);
  585. }
  586. static int vaapi_encode_mangle_end(AVCodecContext *avctx)
  587. {
  588. VAAPIEncodeContext *ctx = avctx->priv_data;
  589. VAAPIEncodePicture *pic, *last_pic, *next;
  590. // Find the last picture we actually have input for.
  591. for (pic = ctx->pic_start; pic; pic = pic->next) {
  592. if (!pic->input_available)
  593. break;
  594. last_pic = pic;
  595. }
  596. if (pic) {
  597. av_assert0(last_pic);
  598. if (last_pic->type == PICTURE_TYPE_B) {
  599. // Some fixing up is required. Change the type of this
  600. // picture to P, then modify preceding B references which
  601. // point beyond it to point at it instead.
  602. last_pic->type = PICTURE_TYPE_P;
  603. last_pic->encode_order = last_pic->refs[1]->encode_order;
  604. for (pic = ctx->pic_start; pic != last_pic; pic = pic->next) {
  605. if (pic->type == PICTURE_TYPE_B &&
  606. pic->refs[1] == last_pic->refs[1])
  607. pic->refs[1] = last_pic;
  608. }
  609. last_pic->nb_refs = 1;
  610. last_pic->refs[1] = NULL;
  611. } else {
  612. // We can use the current structure (no references point
  613. // beyond the end), but there are unused pics to discard.
  614. }
  615. // Discard all following pics, they will never be used.
  616. for (pic = last_pic->next; pic; pic = next) {
  617. next = pic->next;
  618. vaapi_encode_free(avctx, pic);
  619. }
  620. last_pic->next = NULL;
  621. ctx->pic_end = last_pic;
  622. } else {
  623. // Input is available for all pictures, so we don't need to
  624. // mangle anything.
  625. }
  626. av_log(avctx, AV_LOG_DEBUG, "Pictures at end of stream:");
  627. for (pic = ctx->pic_start; pic; pic = pic->next) {
  628. av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
  629. picture_type_name[pic->type],
  630. pic->display_order, pic->encode_order);
  631. }
  632. av_log(avctx, AV_LOG_DEBUG, "\n");
  633. return 0;
  634. }
  635. static int vaapi_encode_clear_old(AVCodecContext *avctx)
  636. {
  637. VAAPIEncodeContext *ctx = avctx->priv_data;
  638. VAAPIEncodePicture *pic, *old;
  639. int i;
  640. while (ctx->pic_start != ctx->pic_end) {
  641. old = ctx->pic_start;
  642. if (old->encode_order > ctx->output_order)
  643. break;
  644. for (pic = old->next; pic; pic = pic->next) {
  645. if (pic->encode_complete)
  646. continue;
  647. for (i = 0; i < pic->nb_refs; i++) {
  648. if (pic->refs[i] == old) {
  649. // We still need this picture because it's referred to
  650. // directly by a later one, so it and all following
  651. // pictures have to stay.
  652. return 0;
  653. }
  654. }
  655. }
  656. pic = ctx->pic_start;
  657. ctx->pic_start = pic->next;
  658. vaapi_encode_free(avctx, pic);
  659. }
  660. return 0;
  661. }
  662. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  663. const AVFrame *input_image, int *got_packet)
  664. {
  665. VAAPIEncodeContext *ctx = avctx->priv_data;
  666. VAAPIEncodePicture *pic;
  667. int err;
  668. if (input_image) {
  669. av_log(avctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n",
  670. input_image->width, input_image->height, input_image->pts);
  671. err = vaapi_encode_get_next(avctx, &pic);
  672. if (err) {
  673. av_log(avctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err);
  674. return err;
  675. }
  676. pic->input_image = av_frame_alloc();
  677. if (!pic->input_image) {
  678. err = AVERROR(ENOMEM);
  679. goto fail;
  680. }
  681. err = av_frame_ref(pic->input_image, input_image);
  682. if (err < 0)
  683. goto fail;
  684. pic->input_surface = (VASurfaceID)(uintptr_t)input_image->data[3];
  685. pic->pts = input_image->pts;
  686. if (ctx->input_order == 0)
  687. ctx->first_pts = pic->pts;
  688. if (ctx->input_order == ctx->decode_delay)
  689. ctx->dts_pts_diff = pic->pts - ctx->first_pts;
  690. if (ctx->output_delay > 0)
  691. ctx->ts_ring[ctx->input_order % (3 * ctx->output_delay)] = pic->pts;
  692. pic->input_available = 1;
  693. } else {
  694. if (!ctx->end_of_stream) {
  695. err = vaapi_encode_mangle_end(avctx);
  696. if (err < 0)
  697. goto fail;
  698. ctx->end_of_stream = 1;
  699. }
  700. }
  701. ++ctx->input_order;
  702. ++ctx->output_order;
  703. av_assert0(ctx->output_order + ctx->output_delay + 1 == ctx->input_order);
  704. for (pic = ctx->pic_start; pic; pic = pic->next)
  705. if (pic->encode_order == ctx->output_order)
  706. break;
  707. // pic can be null here if we don't have a specific target in this
  708. // iteration. We might still issue encodes if things can be overlapped,
  709. // even though we don't intend to output anything.
  710. err = vaapi_encode_step(avctx, pic);
  711. if (err < 0) {
  712. av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
  713. goto fail;
  714. }
  715. if (!pic) {
  716. *got_packet = 0;
  717. } else {
  718. err = vaapi_encode_output(avctx, pic, pkt);
  719. if (err < 0) {
  720. av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
  721. goto fail;
  722. }
  723. if (ctx->output_delay == 0) {
  724. pkt->dts = pkt->pts;
  725. } else if (ctx->output_order < ctx->decode_delay) {
  726. if (ctx->ts_ring[ctx->output_order] < INT64_MIN + ctx->dts_pts_diff)
  727. pkt->dts = INT64_MIN;
  728. else
  729. pkt->dts = ctx->ts_ring[ctx->output_order] - ctx->dts_pts_diff;
  730. } else {
  731. pkt->dts = ctx->ts_ring[(ctx->output_order - ctx->decode_delay) %
  732. (3 * ctx->output_delay)];
  733. }
  734. *got_packet = 1;
  735. }
  736. err = vaapi_encode_clear_old(avctx);
  737. if (err < 0) {
  738. av_log(avctx, AV_LOG_ERROR, "List clearing failed: %d.\n", err);
  739. goto fail;
  740. }
  741. return 0;
  742. fail:
  743. // Unclear what to clean up on failure. There are probably some things we
  744. // could do usefully clean up here, but for now just leave them for uninit()
  745. // to do instead.
  746. return err;
  747. }
  748. av_cold int ff_vaapi_encode_init(AVCodecContext *avctx,
  749. const VAAPIEncodeType *type)
  750. {
  751. VAAPIEncodeContext *ctx = avctx->priv_data;
  752. AVVAAPIFramesContext *recon_hwctx = NULL;
  753. AVVAAPIHWConfig *hwconfig = NULL;
  754. AVHWFramesConstraints *constraints = NULL;
  755. enum AVPixelFormat recon_format;
  756. VAStatus vas;
  757. int err, i;
  758. if (!avctx->hw_frames_ctx) {
  759. av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
  760. "required to associate the encoding device.\n");
  761. return AVERROR(EINVAL);
  762. }
  763. ctx->codec = type;
  764. ctx->codec_options = ctx->codec_options_data;
  765. ctx->priv_data = av_mallocz(type->priv_data_size);
  766. if (!ctx->priv_data) {
  767. err = AVERROR(ENOMEM);
  768. goto fail;
  769. }
  770. ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
  771. if (!ctx->input_frames_ref) {
  772. err = AVERROR(ENOMEM);
  773. goto fail;
  774. }
  775. ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
  776. ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
  777. if (!ctx->device_ref) {
  778. err = AVERROR(ENOMEM);
  779. goto fail;
  780. }
  781. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  782. ctx->hwctx = ctx->device->hwctx;
  783. err = ctx->codec->init(avctx);
  784. if (err < 0)
  785. goto fail;
  786. vas = vaCreateConfig(ctx->hwctx->display,
  787. ctx->va_profile, ctx->va_entrypoint,
  788. ctx->config_attributes, ctx->nb_config_attributes,
  789. &ctx->va_config);
  790. if (vas != VA_STATUS_SUCCESS) {
  791. av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
  792. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  793. err = AVERROR(EIO);
  794. goto fail;
  795. }
  796. hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
  797. if (!hwconfig) {
  798. err = AVERROR(ENOMEM);
  799. goto fail;
  800. }
  801. hwconfig->config_id = ctx->va_config;
  802. constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
  803. hwconfig);
  804. if (!constraints) {
  805. err = AVERROR(ENOMEM);
  806. goto fail;
  807. }
  808. // Probably we can use the input surface format as the surface format
  809. // of the reconstructed frames. If not, we just pick the first (only?)
  810. // format in the valid list and hope that it all works.
  811. recon_format = AV_PIX_FMT_NONE;
  812. if (constraints->valid_sw_formats) {
  813. for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
  814. if (ctx->input_frames->sw_format ==
  815. constraints->valid_sw_formats[i]) {
  816. recon_format = ctx->input_frames->sw_format;
  817. break;
  818. }
  819. }
  820. if (recon_format == AV_PIX_FMT_NONE)
  821. recon_format = constraints->valid_sw_formats[i];
  822. } else {
  823. // No idea what to use; copy input format.
  824. recon_format = ctx->input_frames->sw_format;
  825. }
  826. av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
  827. "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
  828. if (ctx->aligned_width < constraints->min_width ||
  829. ctx->aligned_height < constraints->min_height ||
  830. ctx->aligned_width > constraints->max_width ||
  831. ctx->aligned_height > constraints->max_height) {
  832. av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
  833. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  834. ctx->aligned_width, ctx->aligned_height,
  835. constraints->min_width, constraints->max_width,
  836. constraints->min_height, constraints->max_height);
  837. err = AVERROR(EINVAL);
  838. goto fail;
  839. }
  840. av_freep(&hwconfig);
  841. av_hwframe_constraints_free(&constraints);
  842. ctx->recon_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  843. if (!ctx->recon_frames_ref) {
  844. err = AVERROR(ENOMEM);
  845. goto fail;
  846. }
  847. ctx->recon_frames = (AVHWFramesContext*)ctx->recon_frames_ref->data;
  848. ctx->recon_frames->format = AV_PIX_FMT_VAAPI;
  849. ctx->recon_frames->sw_format = recon_format;
  850. ctx->recon_frames->width = ctx->aligned_width;
  851. ctx->recon_frames->height = ctx->aligned_height;
  852. ctx->recon_frames->initial_pool_size = ctx->nb_recon_frames;
  853. err = av_hwframe_ctx_init(ctx->recon_frames_ref);
  854. if (err < 0) {
  855. av_log(avctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
  856. "frame context: %d.\n", err);
  857. goto fail;
  858. }
  859. recon_hwctx = ctx->recon_frames->hwctx;
  860. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  861. ctx->aligned_width, ctx->aligned_height,
  862. VA_PROGRESSIVE,
  863. recon_hwctx->surface_ids,
  864. recon_hwctx->nb_surfaces,
  865. &ctx->va_context);
  866. if (vas != VA_STATUS_SUCCESS) {
  867. av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
  868. "context: %d (%s).\n", vas, vaErrorStr(vas));
  869. err = AVERROR(EIO);
  870. goto fail;
  871. }
  872. ctx->input_order = 0;
  873. ctx->output_delay = avctx->max_b_frames;
  874. ctx->decode_delay = 1;
  875. ctx->output_order = - ctx->output_delay - 1;
  876. if (ctx->codec->sequence_params_size > 0) {
  877. ctx->codec_sequence_params =
  878. av_mallocz(ctx->codec->sequence_params_size);
  879. if (!ctx->codec_sequence_params) {
  880. err = AVERROR(ENOMEM);
  881. goto fail;
  882. }
  883. }
  884. if (ctx->codec->picture_params_size > 0) {
  885. ctx->codec_picture_params =
  886. av_mallocz(ctx->codec->picture_params_size);
  887. if (!ctx->codec_picture_params) {
  888. err = AVERROR(ENOMEM);
  889. goto fail;
  890. }
  891. }
  892. if (ctx->codec->init_sequence_params) {
  893. err = ctx->codec->init_sequence_params(avctx);
  894. if (err < 0) {
  895. av_log(avctx, AV_LOG_ERROR, "Codec sequence initialisation "
  896. "failed: %d.\n", err);
  897. goto fail;
  898. }
  899. }
  900. // All I are IDR for now.
  901. ctx->i_per_idr = 0;
  902. ctx->p_per_i = ((avctx->gop_size + avctx->max_b_frames) /
  903. (avctx->max_b_frames + 1));
  904. ctx->b_per_p = avctx->max_b_frames;
  905. // This should be configurable somehow. (Needs testing on a machine
  906. // where it actually overlaps properly, though.)
  907. ctx->issue_mode = ISSUE_MODE_MAXIMISE_THROUGHPUT;
  908. return 0;
  909. fail:
  910. av_freep(&hwconfig);
  911. av_hwframe_constraints_free(&constraints);
  912. ff_vaapi_encode_close(avctx);
  913. return err;
  914. }
  915. av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
  916. {
  917. VAAPIEncodeContext *ctx = avctx->priv_data;
  918. VAAPIEncodePicture *pic, *next;
  919. for (pic = ctx->pic_start; pic; pic = next) {
  920. next = pic->next;
  921. vaapi_encode_free(avctx, pic);
  922. }
  923. if (ctx->va_context != VA_INVALID_ID)
  924. vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  925. if (ctx->va_config != VA_INVALID_ID)
  926. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  927. if (ctx->codec->close)
  928. ctx->codec->close(avctx);
  929. av_freep(&ctx->codec_sequence_params);
  930. av_freep(&ctx->codec_picture_params);
  931. av_buffer_unref(&ctx->recon_frames_ref);
  932. av_buffer_unref(&ctx->input_frames_ref);
  933. av_buffer_unref(&ctx->device_ref);
  934. av_freep(&ctx->priv_data);
  935. return 0;
  936. }