You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.8KB

  1. /*
  2. * OpenH264 video decoder
  3. * Copyright (C) 2016 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <wels/codec_api.h>
  22. #include <wels/codec_ver.h>
  23. #include "libavutil/common.h"
  24. #include "libavutil/fifo.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "avcodec.h"
  30. #include "internal.h"
  31. #include "libopenh264.h"
  32. typedef struct SVCContext {
  33. ISVCDecoder *decoder;
  34. } SVCContext;
  35. static av_cold int svc_decode_close(AVCodecContext *avctx)
  36. {
  37. SVCContext *s = avctx->priv_data;
  38. if (s->decoder)
  39. WelsDestroyDecoder(s->decoder);
  40. return 0;
  41. }
  42. static av_cold int svc_decode_init(AVCodecContext *avctx)
  43. {
  44. SVCContext *s = avctx->priv_data;
  45. SDecodingParam param = { 0 };
  46. int err;
  47. int log_level;
  48. WelsTraceCallback callback_function;
  49. if ((err = ff_libopenh264_check_version(avctx)) < 0)
  50. return err;
  51. if (WelsCreateDecoder(&s->decoder)) {
  52. av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
  53. return AVERROR_UNKNOWN;
  54. }
  55. // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
  56. log_level = WELS_LOG_DETAIL;
  57. callback_function = ff_libopenh264_trace_callback;
  58. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_LEVEL, &log_level);
  59. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK, (void *)&callback_function);
  60. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, (void *)&avctx);
  61. #if !OPENH264_VER_AT_LEAST(1, 6)
  62. param.eOutputColorFormat = videoFormatI420;
  63. #endif
  64. param.eEcActiveIdc = ERROR_CON_DISABLE;
  65. param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
  66. if ((*s->decoder)->Initialize(s->decoder, &param) != cmResultSuccess) {
  67. av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
  68. return AVERROR_UNKNOWN;
  69. }
  70. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  71. return 0;
  72. }
  73. static int svc_decode_frame(AVCodecContext *avctx, void *data,
  74. int *got_frame, AVPacket *avpkt)
  75. {
  76. SVCContext *s = avctx->priv_data;
  77. SBufferInfo info = { 0 };
  78. uint8_t* ptrs[3];
  79. int linesize[3];
  80. AVFrame *avframe = data;
  81. DECODING_STATE state;
  82. #if OPENH264_VER_AT_LEAST(1, 7)
  83. int opt;
  84. #endif
  85. if (!avpkt->data) {
  86. #if OPENH264_VER_AT_LEAST(1, 9)
  87. int end_of_stream = 1;
  88. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_END_OF_STREAM, &end_of_stream);
  89. state = (*s->decoder)->FlushFrame(s->decoder, ptrs, &info);
  90. #else
  91. return 0;
  92. #endif
  93. } else {
  94. info.uiInBsTimeStamp = avpkt->pts;
  95. #if OPENH264_VER_AT_LEAST(1, 4)
  96. // Contrary to the name, DecodeFrameNoDelay actually does buffering
  97. // and reordering of frames, and is the recommended decoding entry
  98. // point since 1.4. This is essential for successfully decoding
  99. // B-frames.
  100. state = (*s->decoder)->DecodeFrameNoDelay(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
  101. #else
  102. state = (*s->decoder)->DecodeFrame2(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
  103. #endif
  104. }
  105. if (state != dsErrorFree) {
  106. av_log(avctx, AV_LOG_ERROR, "DecodeFrame failed\n");
  107. return AVERROR_UNKNOWN;
  108. }
  109. if (info.iBufferStatus != 1) {
  110. av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
  111. return avpkt->size;
  112. }
  113. ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
  114. // The decoder doesn't (currently) support decoding into a user
  115. // provided buffer, so do a copy instead.
  116. if (ff_get_buffer(avctx, avframe, 0) < 0) {
  117. av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
  118. return AVERROR(ENOMEM);
  119. }
  120. linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
  121. linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
  122. av_image_copy(avframe->data, avframe->linesize, (const uint8_t **) ptrs, linesize, avctx->pix_fmt, avctx->width, avctx->height);
  123. avframe->pts = info.uiOutYuvTimeStamp;
  124. avframe->pkt_dts = AV_NOPTS_VALUE;
  125. #if FF_API_PKT_PTS
  126. FF_DISABLE_DEPRECATION_WARNINGS
  127. avframe->pkt_pts = avpkt->pts;
  128. FF_ENABLE_DEPRECATION_WARNINGS
  129. #endif
  130. #if OPENH264_VER_AT_LEAST(1, 7)
  131. (*s->decoder)->GetOption(s->decoder, DECODER_OPTION_PROFILE, &opt);
  132. avctx->profile = opt;
  133. (*s->decoder)->GetOption(s->decoder, DECODER_OPTION_LEVEL, &opt);
  134. avctx->level = opt;
  135. #endif
  136. *got_frame = 1;
  137. return avpkt->size;
  138. }
  139. AVCodec ff_libopenh264_decoder = {
  140. .name = "libopenh264",
  141. .long_name = NULL_IF_CONFIG_SMALL("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  142. .type = AVMEDIA_TYPE_VIDEO,
  143. .id = AV_CODEC_ID_H264,
  144. .priv_data_size = sizeof(SVCContext),
  145. .init = svc_decode_init,
  146. .decode = svc_decode_frame,
  147. .close = svc_decode_close,
  148. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  149. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_THREADSAFE |
  150. FF_CODEC_CAP_INIT_CLEANUP,
  151. .bsfs = "h264_mp4toannexb",
  152. .wrapper_name = "libopenh264",
  153. };