vaapi_transcode.c 9.7 KB

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