vaapi_encode.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Video Acceleration API (video encoding) encode sample
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file
  24. * Intel VAAPI-accelerated encoding example.
  25. *
  26. * @example vaapi_encode.c
  27. * This example shows how to do VAAPI-accelerated encoding. now only support NV12
  28. * raw file, usage like: vaapi_encode 1920 1080 input.yuv output.h264
  29. *
  30. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <libavcodec/avcodec.h>
  35. #include <libavutil/pixdesc.h>
  36. #include <libavutil/hwcontext.h>
  37. static int width, height;
  38. static AVBufferRef *hw_device_ctx = NULL;
  39. static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
  40. {
  41. AVBufferRef *hw_frames_ref;
  42. AVHWFramesContext *frames_ctx = NULL;
  43. int err = 0;
  44. if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
  45. fprintf(stderr, "Failed to create VAAPI frame context.\n");
  46. return -1;
  47. }
  48. frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
  49. frames_ctx->format = AV_PIX_FMT_VAAPI;
  50. frames_ctx->sw_format = AV_PIX_FMT_NV12;
  51. frames_ctx->width = width;
  52. frames_ctx->height = height;
  53. frames_ctx->initial_pool_size = 20;
  54. if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
  55. fprintf(stderr, "Failed to initialize VAAPI frame context."
  56. "Error code: %s\n",av_err2str(err));
  57. av_buffer_unref(&hw_frames_ref);
  58. return err;
  59. }
  60. ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
  61. if (!ctx->hw_frames_ctx)
  62. err = AVERROR(ENOMEM);
  63. av_buffer_unref(&hw_frames_ref);
  64. return err;
  65. }
  66. static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
  67. {
  68. int ret = 0;
  69. AVPacket *enc_pkt;
  70. if (!(enc_pkt = av_packet_alloc()))
  71. return AVERROR(ENOMEM);
  72. if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
  73. fprintf(stderr, "Error code: %s\n", av_err2str(ret));
  74. goto end;
  75. }
  76. while (1) {
  77. ret = avcodec_receive_packet(avctx, enc_pkt);
  78. if (ret)
  79. break;
  80. enc_pkt->stream_index = 0;
  81. ret = fwrite(enc_pkt->data, enc_pkt->size, 1, fout);
  82. av_packet_unref(enc_pkt);
  83. }
  84. end:
  85. av_packet_free(&enc_pkt);
  86. ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
  87. return ret;
  88. }
  89. int main(int argc, char *argv[])
  90. {
  91. int size, err;
  92. FILE *fin = NULL, *fout = NULL;
  93. AVFrame *sw_frame = NULL, *hw_frame = NULL;
  94. AVCodecContext *avctx = NULL;
  95. const AVCodec *codec = NULL;
  96. const char *enc_name = "h264_vaapi";
  97. if (argc < 5) {
  98. fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
  99. return -1;
  100. }
  101. width = atoi(argv[1]);
  102. height = atoi(argv[2]);
  103. size = width * height;
  104. if (!(fin = fopen(argv[3], "r"))) {
  105. fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
  106. return -1;
  107. }
  108. if (!(fout = fopen(argv[4], "w+b"))) {
  109. fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
  110. err = -1;
  111. goto close;
  112. }
  113. err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
  114. NULL, NULL, 0);
  115. if (err < 0) {
  116. fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
  117. goto close;
  118. }
  119. if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
  120. fprintf(stderr, "Could not find encoder.\n");
  121. err = -1;
  122. goto close;
  123. }
  124. if (!(avctx = avcodec_alloc_context3(codec))) {
  125. err = AVERROR(ENOMEM);
  126. goto close;
  127. }
  128. avctx->width = width;
  129. avctx->height = height;
  130. avctx->time_base = (AVRational){1, 25};
  131. avctx->framerate = (AVRational){25, 1};
  132. avctx->sample_aspect_ratio = (AVRational){1, 1};
  133. avctx->pix_fmt = AV_PIX_FMT_VAAPI;
  134. /* set hw_frames_ctx for encoder's AVCodecContext */
  135. if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) {
  136. fprintf(stderr, "Failed to set hwframe context.\n");
  137. goto close;
  138. }
  139. if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
  140. fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
  141. goto close;
  142. }
  143. while (1) {
  144. if (!(sw_frame = av_frame_alloc())) {
  145. err = AVERROR(ENOMEM);
  146. goto close;
  147. }
  148. /* read data into software frame, and transfer them into hw frame */
  149. sw_frame->width = width;
  150. sw_frame->height = height;
  151. sw_frame->format = AV_PIX_FMT_NV12;
  152. if ((err = av_frame_get_buffer(sw_frame, 0)) < 0)
  153. goto close;
  154. if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
  155. break;
  156. if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
  157. break;
  158. if (!(hw_frame = av_frame_alloc())) {
  159. err = AVERROR(ENOMEM);
  160. goto close;
  161. }
  162. if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) {
  163. fprintf(stderr, "Error code: %s.\n", av_err2str(err));
  164. goto close;
  165. }
  166. if (!hw_frame->hw_frames_ctx) {
  167. err = AVERROR(ENOMEM);
  168. goto close;
  169. }
  170. if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) {
  171. fprintf(stderr, "Error while transferring frame data to surface."
  172. "Error code: %s.\n", av_err2str(err));
  173. goto close;
  174. }
  175. if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
  176. fprintf(stderr, "Failed to encode.\n");
  177. goto close;
  178. }
  179. av_frame_free(&hw_frame);
  180. av_frame_free(&sw_frame);
  181. }
  182. /* flush encoder */
  183. err = encode_write(avctx, NULL, fout);
  184. if (err == AVERROR_EOF)
  185. err = 0;
  186. close:
  187. if (fin)
  188. fclose(fin);
  189. if (fout)
  190. fclose(fout);
  191. av_frame_free(&sw_frame);
  192. av_frame_free(&hw_frame);
  193. avcodec_free_context(&avctx);
  194. av_buffer_unref(&hw_device_ctx);
  195. return err;
  196. }