qsvdec.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. typedef struct DecodeContext {
  41. AVBufferRef *hw_device_ref;
  42. } DecodeContext;
  43. static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
  44. {
  45. while (*pix_fmts != AV_PIX_FMT_NONE) {
  46. if (*pix_fmts == AV_PIX_FMT_QSV) {
  47. DecodeContext *decode = avctx->opaque;
  48. AVHWFramesContext *frames_ctx;
  49. AVQSVFramesContext *frames_hwctx;
  50. int ret;
  51. /* create a pool of surfaces to be used by the decoder */
  52. avctx->hw_frames_ctx = av_hwframe_ctx_alloc(decode->hw_device_ref);
  53. if (!avctx->hw_frames_ctx)
  54. return AV_PIX_FMT_NONE;
  55. frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  56. frames_hwctx = frames_ctx->hwctx;
  57. frames_ctx->format = AV_PIX_FMT_QSV;
  58. frames_ctx->sw_format = avctx->sw_pix_fmt;
  59. frames_ctx->width = FFALIGN(avctx->coded_width, 32);
  60. frames_ctx->height = FFALIGN(avctx->coded_height, 32);
  61. frames_ctx->initial_pool_size = 32;
  62. frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  63. ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
  64. if (ret < 0)
  65. return AV_PIX_FMT_NONE;
  66. return AV_PIX_FMT_QSV;
  67. }
  68. pix_fmts++;
  69. }
  70. fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
  71. return AV_PIX_FMT_NONE;
  72. }
  73. static int decode_packet(DecodeContext *decode, AVCodecContext *decoder_ctx,
  74. AVFrame *frame, AVFrame *sw_frame,
  75. AVPacket *pkt, AVIOContext *output_ctx)
  76. {
  77. int ret = 0;
  78. ret = avcodec_send_packet(decoder_ctx, pkt);
  79. if (ret < 0) {
  80. fprintf(stderr, "Error during decoding\n");
  81. return ret;
  82. }
  83. while (ret >= 0) {
  84. int i, j;
  85. ret = avcodec_receive_frame(decoder_ctx, frame);
  86. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  87. break;
  88. else if (ret < 0) {
  89. fprintf(stderr, "Error during decoding\n");
  90. return ret;
  91. }
  92. /* A real program would do something useful with the decoded frame here.
  93. * We just retrieve the raw data and write it to a file, which is rather
  94. * useless but pedagogic. */
  95. ret = av_hwframe_transfer_data(sw_frame, frame, 0);
  96. if (ret < 0) {
  97. fprintf(stderr, "Error transferring the data to system memory\n");
  98. goto fail;
  99. }
  100. for (i = 0; i < FF_ARRAY_ELEMS(sw_frame->data) && sw_frame->data[i]; i++)
  101. for (j = 0; j < (sw_frame->height >> (i > 0)); j++)
  102. avio_write(output_ctx, sw_frame->data[i] + j * sw_frame->linesize[i], sw_frame->width);
  103. fail:
  104. av_frame_unref(sw_frame);
  105. av_frame_unref(frame);
  106. if (ret < 0)
  107. return ret;
  108. }
  109. return 0;
  110. }
  111. int main(int argc, char **argv)
  112. {
  113. AVFormatContext *input_ctx = NULL;
  114. AVStream *video_st = NULL;
  115. AVCodecContext *decoder_ctx = NULL;
  116. const AVCodec *decoder;
  117. AVPacket pkt = { 0 };
  118. AVFrame *frame = NULL, *sw_frame = NULL;
  119. DecodeContext decode = { NULL };
  120. AVIOContext *output_ctx = NULL;
  121. int ret, i;
  122. if (argc < 3) {
  123. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  124. return 1;
  125. }
  126. /* open the input file */
  127. ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);
  128. if (ret < 0) {
  129. fprintf(stderr, "Cannot open input file '%s': ", argv[1]);
  130. goto finish;
  131. }
  132. /* find the first H.264 video stream */
  133. for (i = 0; i < input_ctx->nb_streams; i++) {
  134. AVStream *st = input_ctx->streams[i];
  135. if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !video_st)
  136. video_st = st;
  137. else
  138. st->discard = AVDISCARD_ALL;
  139. }
  140. if (!video_st) {
  141. fprintf(stderr, "No H.264 video stream in the input file\n");
  142. goto finish;
  143. }
  144. /* open the hardware device */
  145. ret = av_hwdevice_ctx_create(&decode.hw_device_ref, AV_HWDEVICE_TYPE_QSV,
  146. "auto", NULL, 0);
  147. if (ret < 0) {
  148. fprintf(stderr, "Cannot open the hardware device\n");
  149. goto finish;
  150. }
  151. /* initialize the decoder */
  152. decoder = avcodec_find_decoder_by_name("h264_qsv");
  153. if (!decoder) {
  154. fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
  155. goto finish;
  156. }
  157. decoder_ctx = avcodec_alloc_context3(decoder);
  158. if (!decoder_ctx) {
  159. ret = AVERROR(ENOMEM);
  160. goto finish;
  161. }
  162. decoder_ctx->codec_id = AV_CODEC_ID_H264;
  163. if (video_st->codecpar->extradata_size) {
  164. decoder_ctx->extradata = av_mallocz(video_st->codecpar->extradata_size +
  165. AV_INPUT_BUFFER_PADDING_SIZE);
  166. if (!decoder_ctx->extradata) {
  167. ret = AVERROR(ENOMEM);
  168. goto finish;
  169. }
  170. memcpy(decoder_ctx->extradata, video_st->codecpar->extradata,
  171. video_st->codecpar->extradata_size);
  172. decoder_ctx->extradata_size = video_st->codecpar->extradata_size;
  173. }
  174. decoder_ctx->opaque = &decode;
  175. decoder_ctx->get_format = get_format;
  176. ret = avcodec_open2(decoder_ctx, NULL, NULL);
  177. if (ret < 0) {
  178. fprintf(stderr, "Error opening the decoder: ");
  179. goto finish;
  180. }
  181. /* open the output stream */
  182. ret = avio_open(&output_ctx, argv[2], AVIO_FLAG_WRITE);
  183. if (ret < 0) {
  184. fprintf(stderr, "Error opening the output context: ");
  185. goto finish;
  186. }
  187. frame = av_frame_alloc();
  188. sw_frame = av_frame_alloc();
  189. if (!frame || !sw_frame) {
  190. ret = AVERROR(ENOMEM);
  191. goto finish;
  192. }
  193. /* actual decoding */
  194. while (ret >= 0) {
  195. ret = av_read_frame(input_ctx, &pkt);
  196. if (ret < 0)
  197. break;
  198. if (pkt.stream_index == video_st->index)
  199. ret = decode_packet(&decode, decoder_ctx, frame, sw_frame, &pkt, output_ctx);
  200. av_packet_unref(&pkt);
  201. }
  202. /* flush the decoder */
  203. pkt.data = NULL;
  204. pkt.size = 0;
  205. ret = decode_packet(&decode, decoder_ctx, frame, sw_frame, &pkt, output_ctx);
  206. finish:
  207. if (ret < 0) {
  208. char buf[1024];
  209. av_strerror(ret, buf, sizeof(buf));
  210. fprintf(stderr, "%s\n", buf);
  211. }
  212. avformat_close_input(&input_ctx);
  213. av_frame_free(&frame);
  214. av_frame_free(&sw_frame);
  215. avcodec_free_context(&decoder_ctx);
  216. av_buffer_unref(&decode.hw_device_ref);
  217. avio_close(output_ctx);
  218. return ret;
  219. }