hw_decode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2017 Jun Zhao
  3. * Copyright (c) 2017 Kaixuan Liu
  4. *
  5. * HW Acceleration API (video decoding) decode sample
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. /**
  26. * @file HW-accelerated decoding API usage.example
  27. * @example hw_decode.c
  28. *
  29. * Perform HW-accelerated decoding with output frames from HW video
  30. * surfaces.
  31. */
  32. #include <stdio.h>
  33. #include <libavcodec/avcodec.h>
  34. #include <libavformat/avformat.h>
  35. #include <libavutil/pixdesc.h>
  36. #include <libavutil/hwcontext.h>
  37. #include <libavutil/opt.h>
  38. #include <libavutil/avassert.h>
  39. #include <libavutil/imgutils.h>
  40. static AVBufferRef *hw_device_ctx = NULL;
  41. static enum AVPixelFormat hw_pix_fmt;
  42. static FILE *output_file = NULL;
  43. static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
  44. {
  45. int err = 0;
  46. if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
  47. NULL, NULL, 0)) < 0) {
  48. fprintf(stderr, "Failed to create specified HW device.\n");
  49. return err;
  50. }
  51. ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  52. return err;
  53. }
  54. static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
  55. const enum AVPixelFormat *pix_fmts)
  56. {
  57. const enum AVPixelFormat *p;
  58. for (p = pix_fmts; *p != -1; p++) {
  59. if (*p == hw_pix_fmt)
  60. return *p;
  61. }
  62. fprintf(stderr, "Failed to get HW surface format.\n");
  63. return AV_PIX_FMT_NONE;
  64. }
  65. static int decode_write(AVCodecContext *avctx, AVPacket *packet)
  66. {
  67. AVFrame *frame = NULL, *sw_frame = NULL;
  68. AVFrame *tmp_frame = NULL;
  69. uint8_t *buffer = NULL;
  70. int size;
  71. int ret = 0;
  72. ret = avcodec_send_packet(avctx, packet);
  73. if (ret < 0) {
  74. fprintf(stderr, "Error during decoding\n");
  75. return ret;
  76. }
  77. while (1) {
  78. if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
  79. fprintf(stderr, "Can not alloc frame\n");
  80. ret = AVERROR(ENOMEM);
  81. goto fail;
  82. }
  83. ret = avcodec_receive_frame(avctx, frame);
  84. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  85. av_frame_free(&frame);
  86. av_frame_free(&sw_frame);
  87. return 0;
  88. } else if (ret < 0) {
  89. fprintf(stderr, "Error while decoding\n");
  90. goto fail;
  91. }
  92. if (frame->format == hw_pix_fmt) {
  93. /* retrieve data from GPU to CPU */
  94. if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
  95. fprintf(stderr, "Error transferring the data to system memory\n");
  96. goto fail;
  97. }
  98. tmp_frame = sw_frame;
  99. } else
  100. tmp_frame = frame;
  101. size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width,
  102. tmp_frame->height, 1);
  103. buffer = av_malloc(size);
  104. if (!buffer) {
  105. fprintf(stderr, "Can not alloc buffer\n");
  106. ret = AVERROR(ENOMEM);
  107. goto fail;
  108. }
  109. ret = av_image_copy_to_buffer(buffer, size,
  110. (const uint8_t * const *)tmp_frame->data,
  111. (const int *)tmp_frame->linesize, tmp_frame->format,
  112. tmp_frame->width, tmp_frame->height, 1);
  113. if (ret < 0) {
  114. fprintf(stderr, "Can not copy image to buffer\n");
  115. goto fail;
  116. }
  117. if ((ret = fwrite(buffer, 1, size, output_file)) < 0) {
  118. fprintf(stderr, "Failed to dump raw data.\n");
  119. goto fail;
  120. }
  121. fail:
  122. av_frame_free(&frame);
  123. av_frame_free(&sw_frame);
  124. av_freep(&buffer);
  125. if (ret < 0)
  126. return ret;
  127. }
  128. }
  129. int main(int argc, char *argv[])
  130. {
  131. AVFormatContext *input_ctx = NULL;
  132. int video_stream, ret;
  133. AVStream *video = NULL;
  134. AVCodecContext *decoder_ctx = NULL;
  135. const AVCodec *decoder = NULL;
  136. AVPacket *packet = NULL;
  137. enum AVHWDeviceType type;
  138. int i;
  139. if (argc < 4) {
  140. fprintf(stderr, "Usage: %s <device type> <input file> <output file>\n", argv[0]);
  141. return -1;
  142. }
  143. type = av_hwdevice_find_type_by_name(argv[1]);
  144. if (type == AV_HWDEVICE_TYPE_NONE) {
  145. fprintf(stderr, "Device type %s is not supported.\n", argv[1]);
  146. fprintf(stderr, "Available device types:");
  147. while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
  148. fprintf(stderr, " %s", av_hwdevice_get_type_name(type));
  149. fprintf(stderr, "\n");
  150. return -1;
  151. }
  152. packet = av_packet_alloc();
  153. if (!packet) {
  154. fprintf(stderr, "Failed to allocate AVPacket\n");
  155. return -1;
  156. }
  157. /* open the input file */
  158. if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) {
  159. fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
  160. return -1;
  161. }
  162. if (avformat_find_stream_info(input_ctx, NULL) < 0) {
  163. fprintf(stderr, "Cannot find input stream information.\n");
  164. return -1;
  165. }
  166. /* find the video stream information */
  167. ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
  168. if (ret < 0) {
  169. fprintf(stderr, "Cannot find a video stream in the input file\n");
  170. return -1;
  171. }
  172. video_stream = ret;
  173. for (i = 0;; i++) {
  174. const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
  175. if (!config) {
  176. fprintf(stderr, "Decoder %s does not support device type %s.\n",
  177. decoder->name, av_hwdevice_get_type_name(type));
  178. return -1;
  179. }
  180. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  181. config->device_type == type) {
  182. hw_pix_fmt = config->pix_fmt;
  183. break;
  184. }
  185. }
  186. if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
  187. return AVERROR(ENOMEM);
  188. video = input_ctx->streams[video_stream];
  189. if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
  190. return -1;
  191. decoder_ctx->get_format = get_hw_format;
  192. if (hw_decoder_init(decoder_ctx, type) < 0)
  193. return -1;
  194. if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) {
  195. fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);
  196. return -1;
  197. }
  198. /* open the file to dump raw data */
  199. output_file = fopen(argv[3], "w+b");
  200. /* actual decoding and dump the raw data */
  201. while (ret >= 0) {
  202. if ((ret = av_read_frame(input_ctx, packet)) < 0)
  203. break;
  204. if (video_stream == packet->stream_index)
  205. ret = decode_write(decoder_ctx, packet);
  206. av_packet_unref(packet);
  207. }
  208. /* flush the decoder */
  209. ret = decode_write(decoder_ctx, NULL);
  210. if (output_file)
  211. fclose(output_file);
  212. av_packet_free(&packet);
  213. avcodec_free_context(&decoder_ctx);
  214. avformat_close_input(&input_ctx);
  215. av_buffer_unref(&hw_device_ctx);
  216. return 0;
  217. }