vaapi_transcode.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Video Acceleration API (video transcoding) transcode sample
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Intel VAAPI-accelerated transcoding example.
  23. *
  24. * @example vaapi_transcode.c
  25. * This example shows how to do VAAPI-accelerated transcoding.
  26. * Usage: vaapi_transcode input_stream codec output_stream
  27. * e.g: - vaapi_transcode input.mp4 h264_vaapi output_h264.mp4
  28. * - vaapi_transcode input.mp4 vp9_vaapi output_vp9.ivf
  29. */
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #include <libavutil/hwcontext.h>
  33. #include <libavcodec/avcodec.h>
  34. #include <libavformat/avformat.h>
  35. static AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
  36. static AVBufferRef *hw_device_ctx = NULL;
  37. static AVCodecContext *decoder_ctx = NULL, *encoder_ctx = NULL;
  38. static int video_stream = -1;
  39. static AVStream *ost;
  40. static int initialized = 0;
  41. static enum AVPixelFormat get_vaapi_format(AVCodecContext *ctx,
  42. const enum AVPixelFormat *pix_fmts)
  43. {
  44. const enum AVPixelFormat *p;
  45. for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
  46. if (*p == AV_PIX_FMT_VAAPI)
  47. return *p;
  48. }
  49. fprintf(stderr, "Unable to decode this file using VA-API.\n");
  50. return AV_PIX_FMT_NONE;
  51. }
  52. static int open_input_file(const char *filename)
  53. {
  54. int ret;
  55. AVCodec *decoder = NULL;
  56. AVStream *video = NULL;
  57. if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
  58. fprintf(stderr, "Cannot open input file '%s', Error code: %s\n",
  59. filename, av_err2str(ret));
  60. return ret;
  61. }
  62. if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
  63. fprintf(stderr, "Cannot find input stream information. Error code: %s\n",
  64. av_err2str(ret));
  65. return ret;
  66. }
  67. ret = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
  68. if (ret < 0) {
  69. fprintf(stderr, "Cannot find a video stream in the input file. "
  70. "Error code: %s\n", av_err2str(ret));
  71. return ret;
  72. }
  73. video_stream = ret;
  74. if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
  75. return AVERROR(ENOMEM);
  76. video = ifmt_ctx->streams[video_stream];
  77. if ((ret = avcodec_parameters_to_context(decoder_ctx, video->codecpar)) < 0) {
  78. fprintf(stderr, "avcodec_parameters_to_context error. Error code: %s\n",
  79. av_err2str(ret));
  80. return ret;
  81. }
  82. decoder_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  83. if (!decoder_ctx->hw_device_ctx) {
  84. fprintf(stderr, "A hardware device reference create failed.\n");
  85. return AVERROR(ENOMEM);
  86. }
  87. decoder_ctx->get_format = get_vaapi_format;
  88. if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0)
  89. fprintf(stderr, "Failed to open codec for decoding. Error code: %s\n",
  90. av_err2str(ret));
  91. return ret;
  92. }
  93. static int encode_write(AVFrame *frame)
  94. {
  95. int ret = 0;
  96. AVPacket enc_pkt;
  97. av_init_packet(&enc_pkt);
  98. enc_pkt.data = NULL;
  99. enc_pkt.size = 0;
  100. if ((ret = avcodec_send_frame(encoder_ctx, frame)) < 0) {
  101. fprintf(stderr, "Error during encoding. Error code: %s\n", av_err2str(ret));
  102. goto end;
  103. }
  104. while (1) {
  105. ret = avcodec_receive_packet(encoder_ctx, &enc_pkt);
  106. if (ret)
  107. break;
  108. enc_pkt.stream_index = 0;
  109. av_packet_rescale_ts(&enc_pkt, ifmt_ctx->streams[video_stream]->time_base,
  110. ofmt_ctx->streams[0]->time_base);
  111. ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
  112. if (ret < 0) {
  113. fprintf(stderr, "Error during writing data to output file. "
  114. "Error code: %s\n", av_err2str(ret));
  115. return -1;
  116. }
  117. }
  118. end:
  119. if (ret == AVERROR_EOF)
  120. return 0;
  121. ret = ((ret == AVERROR(EAGAIN)) ? 0:-1);
  122. return ret;
  123. }
  124. static int dec_enc(AVPacket *pkt, AVCodec *enc_codec)
  125. {
  126. AVFrame *frame;
  127. int ret = 0;
  128. ret = avcodec_send_packet(decoder_ctx, pkt);
  129. if (ret < 0) {
  130. fprintf(stderr, "Error during decoding. Error code: %s\n", av_err2str(ret));
  131. return ret;
  132. }
  133. while (ret >= 0) {
  134. if (!(frame = av_frame_alloc()))
  135. return AVERROR(ENOMEM);
  136. ret = avcodec_receive_frame(decoder_ctx, frame);
  137. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  138. av_frame_free(&frame);
  139. return 0;
  140. } else if (ret < 0) {
  141. fprintf(stderr, "Error while decoding. Error code: %s\n", av_err2str(ret));
  142. goto fail;
  143. }
  144. if (!initialized) {
  145. /* we need to ref hw_frames_ctx of decoder to initialize encoder's codec.
  146. Only after we get a decoded frame, can we obtain its hw_frames_ctx */
  147. encoder_ctx->hw_frames_ctx = av_buffer_ref(decoder_ctx->hw_frames_ctx);
  148. if (!encoder_ctx->hw_frames_ctx) {
  149. ret = AVERROR(ENOMEM);
  150. goto fail;
  151. }
  152. /* set AVCodecContext Parameters for encoder, here we keep them stay
  153. * the same as decoder.
  154. * xxx: now the sample can't handle resolution change case.
  155. */
  156. encoder_ctx->time_base = av_inv_q(decoder_ctx->framerate);
  157. encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI;
  158. encoder_ctx->width = decoder_ctx->width;
  159. encoder_ctx->height = decoder_ctx->height;
  160. if ((ret = avcodec_open2(encoder_ctx, enc_codec, NULL)) < 0) {
  161. fprintf(stderr, "Failed to open encode codec. Error code: %s\n",
  162. av_err2str(ret));
  163. goto fail;
  164. }
  165. if (!(ost = avformat_new_stream(ofmt_ctx, enc_codec))) {
  166. fprintf(stderr, "Failed to allocate stream for output format.\n");
  167. ret = AVERROR(ENOMEM);
  168. goto fail;
  169. }
  170. ost->time_base = encoder_ctx->time_base;
  171. ret = avcodec_parameters_from_context(ost->codecpar, encoder_ctx);
  172. if (ret < 0) {
  173. fprintf(stderr, "Failed to copy the stream parameters. "
  174. "Error code: %s\n", av_err2str(ret));
  175. goto fail;
  176. }
  177. /* write the stream header */
  178. if ((ret = avformat_write_header(ofmt_ctx, NULL)) < 0) {
  179. fprintf(stderr, "Error while writing stream header. "
  180. "Error code: %s\n", av_err2str(ret));
  181. goto fail;
  182. }
  183. initialized = 1;
  184. }
  185. if ((ret = encode_write(frame)) < 0)
  186. fprintf(stderr, "Error during encoding and writing.\n");
  187. fail:
  188. av_frame_free(&frame);
  189. if (ret < 0)
  190. return ret;
  191. }
  192. return 0;
  193. }
  194. int main(int argc, char **argv)
  195. {
  196. int ret = 0;
  197. AVPacket dec_pkt;
  198. AVCodec *enc_codec;
  199. if (argc != 4) {
  200. fprintf(stderr, "Usage: %s <input file> <encode codec> <output file>\n"
  201. "The output format is guessed according to the file extension.\n"
  202. "\n", argv[0]);
  203. return -1;
  204. }
  205. ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, NULL, NULL, 0);
  206. if (ret < 0) {
  207. fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(ret));
  208. return -1;
  209. }
  210. if ((ret = open_input_file(argv[1])) < 0)
  211. goto end;
  212. if (!(enc_codec = avcodec_find_encoder_by_name(argv[2]))) {
  213. fprintf(stderr, "Could not find encoder '%s'\n", argv[2]);
  214. ret = -1;
  215. goto end;
  216. }
  217. if ((ret = (avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, argv[3]))) < 0) {
  218. fprintf(stderr, "Failed to deduce output format from file extension. Error code: "
  219. "%s\n", av_err2str(ret));
  220. goto end;
  221. }
  222. if (!(encoder_ctx = avcodec_alloc_context3(enc_codec))) {
  223. ret = AVERROR(ENOMEM);
  224. goto end;
  225. }
  226. ret = avio_open(&ofmt_ctx->pb, argv[3], AVIO_FLAG_WRITE);
  227. if (ret < 0) {
  228. fprintf(stderr, "Cannot open output file. "
  229. "Error code: %s\n", av_err2str(ret));
  230. goto end;
  231. }
  232. /* read all packets and only transcoding video */
  233. while (ret >= 0) {
  234. if ((ret = av_read_frame(ifmt_ctx, &dec_pkt)) < 0)
  235. break;
  236. if (video_stream == dec_pkt.stream_index)
  237. ret = dec_enc(&dec_pkt, enc_codec);
  238. av_packet_unref(&dec_pkt);
  239. }
  240. /* flush decoder */
  241. dec_pkt.data = NULL;
  242. dec_pkt.size = 0;
  243. ret = dec_enc(&dec_pkt, enc_codec);
  244. av_packet_unref(&dec_pkt);
  245. /* flush encoder */
  246. ret = encode_write(NULL);
  247. /* write the trailer for output stream */
  248. av_write_trailer(ofmt_ctx);
  249. end:
  250. avformat_close_input(&ifmt_ctx);
  251. avformat_close_input(&ofmt_ctx);
  252. avcodec_free_context(&decoder_ctx);
  253. avcodec_free_context(&encoder_ctx);
  254. av_buffer_unref(&hw_device_ctx);
  255. return ret;
  256. }