vaapi_encode.c 7.0 KB

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