qsvdec.c 6.9 KB

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