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.

246 lines
7.6KB

  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. AVBSFContext *bsf;
  35. AVFifoBuffer *packet_fifo;
  36. AVPacket pkt_filtered;
  37. } SVCContext;
  38. static av_cold int svc_decode_close(AVCodecContext *avctx)
  39. {
  40. SVCContext *s = avctx->priv_data;
  41. AVPacket pkt;
  42. if (s->decoder)
  43. WelsDestroyDecoder(s->decoder);
  44. while (av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
  45. av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
  46. av_packet_unref(&pkt);
  47. }
  48. av_bsf_free(&s->bsf);
  49. av_packet_unref(&s->pkt_filtered);
  50. av_fifo_free(s->packet_fifo);
  51. return 0;
  52. }
  53. static av_cold int svc_decode_init(AVCodecContext *avctx)
  54. {
  55. SVCContext *s = avctx->priv_data;
  56. SDecodingParam param = { 0 };
  57. int err;
  58. int log_level;
  59. WelsTraceCallback callback_function;
  60. if ((err = ff_libopenh264_check_version(avctx)) < 0)
  61. return err;
  62. s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
  63. if (!s->packet_fifo) {
  64. err = AVERROR(ENOMEM);
  65. goto fail;
  66. }
  67. if (WelsCreateDecoder(&s->decoder)) {
  68. av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
  69. err = AVERROR_UNKNOWN;
  70. goto fail;
  71. }
  72. // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
  73. log_level = WELS_LOG_DETAIL;
  74. callback_function = ff_libopenh264_trace_callback;
  75. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_LEVEL, &log_level);
  76. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK, (void *)&callback_function);
  77. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, (void *)&avctx);
  78. param.eOutputColorFormat = videoFormatI420;
  79. param.eEcActiveIdc = ERROR_CON_DISABLE;
  80. param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
  81. if ((*s->decoder)->Initialize(s->decoder, &param) != cmResultSuccess) {
  82. av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
  83. err = AVERROR_UNKNOWN;
  84. goto fail;
  85. }
  86. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  87. fail:
  88. return err;
  89. }
  90. static int init_bsf(AVCodecContext *avctx)
  91. {
  92. SVCContext *s = avctx->priv_data;
  93. const AVBitStreamFilter *filter;
  94. int ret;
  95. if (s->bsf)
  96. return 0;
  97. // If the input stream already is annex b, this BSF only passes the
  98. // packets through unchanged.
  99. filter = av_bsf_get_by_name("h264_mp4toannexb");
  100. if (!filter)
  101. return AVERROR_BUG;
  102. ret = av_bsf_alloc(filter, &s->bsf);
  103. if (ret < 0)
  104. return ret;
  105. ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
  106. if (ret < 0)
  107. return ret;
  108. s->bsf->time_base_in = avctx->time_base;
  109. ret = av_bsf_init(s->bsf);
  110. if (ret < 0)
  111. return ret;
  112. return ret;
  113. }
  114. static int svc_decode_frame(AVCodecContext *avctx, void *data,
  115. int *got_frame, AVPacket *avpkt)
  116. {
  117. SVCContext *s = avctx->priv_data;
  118. SBufferInfo info = { 0 };
  119. uint8_t* ptrs[3];
  120. int linesize[3];
  121. AVFrame *avframe = data;
  122. int ret;
  123. DECODING_STATE state;
  124. if ((ret = init_bsf(avctx)) < 0)
  125. return ret;
  126. if (avpkt->size) {
  127. AVPacket input_ref = { 0 };
  128. if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
  129. ret = av_fifo_realloc2(s->packet_fifo,
  130. av_fifo_size(s->packet_fifo) + sizeof(input_ref));
  131. if (ret < 0)
  132. return ret;
  133. }
  134. ret = av_packet_ref(&input_ref, avpkt);
  135. if (ret < 0)
  136. return ret;
  137. av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
  138. }
  139. while (!*got_frame) {
  140. /* prepare the input data -- convert to Annex B if needed */
  141. if (s->pkt_filtered.size <= 0) {
  142. AVPacket input_ref;
  143. /* no more data */
  144. if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
  145. return avpkt->size ? avpkt->size : 0;
  146. av_packet_unref(&s->pkt_filtered);
  147. av_fifo_generic_read(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
  148. ret = av_bsf_send_packet(s->bsf, &input_ref);
  149. if (ret < 0) {
  150. av_packet_unref(&input_ref);
  151. return ret;
  152. }
  153. ret = av_bsf_receive_packet(s->bsf, &s->pkt_filtered);
  154. if (ret < 0)
  155. av_packet_move_ref(&s->pkt_filtered, &input_ref);
  156. else
  157. av_packet_unref(&input_ref);
  158. }
  159. state = (*s->decoder)->DecodeFrame2(s->decoder, s->pkt_filtered.data, s->pkt_filtered.size, ptrs, &info);
  160. s->pkt_filtered.size = 0;
  161. if (state != dsErrorFree) {
  162. av_log(avctx, AV_LOG_ERROR, "DecodeFrame2 failed\n");
  163. return AVERROR_UNKNOWN;
  164. }
  165. if (info.iBufferStatus != 1) {
  166. av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
  167. continue;
  168. }
  169. ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
  170. // The decoder doesn't (currently) support decoding into a user
  171. // provided buffer, so do a copy instead.
  172. if (ff_get_buffer(avctx, avframe, 0) < 0) {
  173. av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
  174. return AVERROR(ENOMEM);
  175. }
  176. linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
  177. linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
  178. av_image_copy(avframe->data, avframe->linesize, (const uint8_t **) ptrs, linesize, avctx->pix_fmt, avctx->width, avctx->height);
  179. avframe->pts = s->pkt_filtered.pts;
  180. avframe->pkt_dts = s->pkt_filtered.dts;
  181. #if FF_API_PKT_PTS
  182. FF_DISABLE_DEPRECATION_WARNINGS
  183. avframe->pkt_pts = s->pkt_filtered.pts;
  184. FF_ENABLE_DEPRECATION_WARNINGS
  185. #endif
  186. *got_frame = 1;
  187. }
  188. return avpkt->size;
  189. }
  190. AVCodec ff_libopenh264_decoder = {
  191. .name = "libopenh264",
  192. .long_name = NULL_IF_CONFIG_SMALL("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  193. .type = AVMEDIA_TYPE_VIDEO,
  194. .id = AV_CODEC_ID_H264,
  195. .priv_data_size = sizeof(SVCContext),
  196. .init = svc_decode_init,
  197. .decode = svc_decode_frame,
  198. .close = svc_decode_close,
  199. // The decoder doesn't currently support B-frames, and the decoder's API
  200. // doesn't support reordering/delay, but the BSF could incur delay.
  201. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  202. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_THREADSAFE |
  203. FF_CODEC_CAP_INIT_CLEANUP,
  204. };