qsv_decode.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2015 Anton Khirnov
  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 Intel QSV-accelerated H.264 decoding API usage example
  24. * @example qsv_decode.c
  25. *
  26. * Perform QSV-accelerated H.264 decoding with output frames in the
  27. * GPU video surfaces, write the decoded frames to an output file.
  28. */
  29. #include <stdio.h>
  30. #include <libavformat/avformat.h>
  31. #include <libavformat/avio.h>
  32. #include <libavcodec/avcodec.h>
  33. #include <libavutil/buffer.h>
  34. #include <libavutil/error.h>
  35. #include <libavutil/hwcontext.h>
  36. #include <libavutil/hwcontext_qsv.h>
  37. #include <libavutil/mem.h>
  38. static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
  39. {
  40. while (*pix_fmts != AV_PIX_FMT_NONE) {
  41. if (*pix_fmts == AV_PIX_FMT_QSV) {
  42. return AV_PIX_FMT_QSV;
  43. }
  44. pix_fmts++;
  45. }
  46. fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
  47. return AV_PIX_FMT_NONE;
  48. }
  49. static int decode_packet(AVCodecContext *decoder_ctx,
  50. AVFrame *frame, AVFrame *sw_frame,
  51. AVPacket *pkt, AVIOContext *output_ctx)
  52. {
  53. int ret = 0;
  54. ret = avcodec_send_packet(decoder_ctx, pkt);
  55. if (ret < 0) {
  56. fprintf(stderr, "Error during decoding\n");
  57. return ret;
  58. }
  59. while (ret >= 0) {
  60. int i, j;
  61. ret = avcodec_receive_frame(decoder_ctx, frame);
  62. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  63. break;
  64. else if (ret < 0) {
  65. fprintf(stderr, "Error during decoding\n");
  66. return ret;
  67. }
  68. /* A real program would do something useful with the decoded frame here.
  69. * We just retrieve the raw data and write it to a file, which is rather
  70. * useless but pedagogic. */
  71. ret = av_hwframe_transfer_data(sw_frame, frame, 0);
  72. if (ret < 0) {
  73. fprintf(stderr, "Error transferring the data to system memory\n");
  74. goto fail;
  75. }
  76. for (i = 0; i < FF_ARRAY_ELEMS(sw_frame->data) && sw_frame->data[i]; i++)
  77. for (j = 0; j < (sw_frame->height >> (i > 0)); j++)
  78. avio_write(output_ctx, sw_frame->data[i] + j * sw_frame->linesize[i], sw_frame->width);
  79. fail:
  80. av_frame_unref(sw_frame);
  81. av_frame_unref(frame);
  82. if (ret < 0)
  83. return ret;
  84. }
  85. return 0;
  86. }
  87. int main(int argc, char **argv)
  88. {
  89. AVFormatContext *input_ctx = NULL;
  90. AVStream *video_st = NULL;
  91. AVCodecContext *decoder_ctx = NULL;
  92. const AVCodec *decoder;
  93. AVPacket *pkt = NULL;
  94. AVFrame *frame = NULL, *sw_frame = NULL;
  95. AVIOContext *output_ctx = NULL;
  96. int ret, i;
  97. AVBufferRef *device_ref = NULL;
  98. if (argc < 3) {
  99. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  100. return 1;
  101. }
  102. /* open the input file */
  103. ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);
  104. if (ret < 0) {
  105. fprintf(stderr, "Cannot open input file '%s': ", argv[1]);
  106. goto finish;
  107. }
  108. /* find the first H.264 video stream */
  109. for (i = 0; i < input_ctx->nb_streams; i++) {
  110. AVStream *st = input_ctx->streams[i];
  111. if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !video_st)
  112. video_st = st;
  113. else
  114. st->discard = AVDISCARD_ALL;
  115. }
  116. if (!video_st) {
  117. fprintf(stderr, "No H.264 video stream in the input file\n");
  118. goto finish;
  119. }
  120. /* open the hardware device */
  121. ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_QSV,
  122. "auto", NULL, 0);
  123. if (ret < 0) {
  124. fprintf(stderr, "Cannot open the hardware device\n");
  125. goto finish;
  126. }
  127. /* initialize the decoder */
  128. decoder = avcodec_find_decoder_by_name("h264_qsv");
  129. if (!decoder) {
  130. fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
  131. goto finish;
  132. }
  133. decoder_ctx = avcodec_alloc_context3(decoder);
  134. if (!decoder_ctx) {
  135. ret = AVERROR(ENOMEM);
  136. goto finish;
  137. }
  138. decoder_ctx->codec_id = AV_CODEC_ID_H264;
  139. if (video_st->codecpar->extradata_size) {
  140. decoder_ctx->extradata = av_mallocz(video_st->codecpar->extradata_size +
  141. AV_INPUT_BUFFER_PADDING_SIZE);
  142. if (!decoder_ctx->extradata) {
  143. ret = AVERROR(ENOMEM);
  144. goto finish;
  145. }
  146. memcpy(decoder_ctx->extradata, video_st->codecpar->extradata,
  147. video_st->codecpar->extradata_size);
  148. decoder_ctx->extradata_size = video_st->codecpar->extradata_size;
  149. }
  150. decoder_ctx->hw_device_ctx = av_buffer_ref(device_ref);
  151. decoder_ctx->get_format = get_format;
  152. ret = avcodec_open2(decoder_ctx, NULL, NULL);
  153. if (ret < 0) {
  154. fprintf(stderr, "Error opening the decoder: ");
  155. goto finish;
  156. }
  157. /* open the output stream */
  158. ret = avio_open(&output_ctx, argv[2], AVIO_FLAG_WRITE);
  159. if (ret < 0) {
  160. fprintf(stderr, "Error opening the output context: ");
  161. goto finish;
  162. }
  163. frame = av_frame_alloc();
  164. sw_frame = av_frame_alloc();
  165. pkt = av_packet_alloc();
  166. if (!frame || !sw_frame || !pkt) {
  167. ret = AVERROR(ENOMEM);
  168. goto finish;
  169. }
  170. /* actual decoding */
  171. while (ret >= 0) {
  172. ret = av_read_frame(input_ctx, pkt);
  173. if (ret < 0)
  174. break;
  175. if (pkt->stream_index == video_st->index)
  176. ret = decode_packet(decoder_ctx, frame, sw_frame, pkt, output_ctx);
  177. av_packet_unref(pkt);
  178. }
  179. /* flush the decoder */
  180. ret = decode_packet(decoder_ctx, frame, sw_frame, NULL, output_ctx);
  181. finish:
  182. if (ret < 0) {
  183. char buf[1024];
  184. av_strerror(ret, buf, sizeof(buf));
  185. fprintf(stderr, "%s\n", buf);
  186. }
  187. avformat_close_input(&input_ctx);
  188. av_frame_free(&frame);
  189. av_frame_free(&sw_frame);
  190. av_packet_free(&pkt);
  191. avcodec_free_context(&decoder_ctx);
  192. av_buffer_unref(&device_ref);
  193. avio_close(output_ctx);
  194. return ret;
  195. }