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.

223 lines
5.7KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <string.h>
  19. #include "libavutil/log.h"
  20. #include "libavutil/mem.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/avassert.h"
  23. #include "avcodec.h"
  24. #include "bsf.h"
  25. struct AVBSFInternal {
  26. AVPacket *buffer_pkt;
  27. int eof;
  28. };
  29. void av_bsf_free(AVBSFContext **pctx)
  30. {
  31. AVBSFContext *ctx;
  32. if (!pctx || !*pctx)
  33. return;
  34. ctx = *pctx;
  35. if (ctx->filter->close)
  36. ctx->filter->close(ctx);
  37. if (ctx->filter->priv_class && ctx->priv_data)
  38. av_opt_free(ctx->priv_data);
  39. av_opt_free(ctx);
  40. av_packet_free(&ctx->internal->buffer_pkt);
  41. av_freep(&ctx->internal);
  42. av_freep(&ctx->priv_data);
  43. avcodec_parameters_free(&ctx->par_in);
  44. avcodec_parameters_free(&ctx->par_out);
  45. av_freep(pctx);
  46. }
  47. static void *bsf_child_next(void *obj, void *prev)
  48. {
  49. AVBSFContext *ctx = obj;
  50. if (!prev && ctx->filter->priv_class)
  51. return ctx->priv_data;
  52. return NULL;
  53. }
  54. static const AVClass bsf_class = {
  55. .class_name = "AVBSFContext",
  56. .item_name = av_default_item_name,
  57. .version = LIBAVUTIL_VERSION_INT,
  58. .child_next = bsf_child_next,
  59. .child_class_next = ff_bsf_child_class_next,
  60. };
  61. const AVClass *av_bsf_get_class(void)
  62. {
  63. return &bsf_class;
  64. }
  65. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
  66. {
  67. AVBSFContext *ctx;
  68. int ret;
  69. ctx = av_mallocz(sizeof(*ctx));
  70. if (!ctx)
  71. return AVERROR(ENOMEM);
  72. ctx->av_class = &bsf_class;
  73. ctx->filter = filter;
  74. ctx->par_in = avcodec_parameters_alloc();
  75. ctx->par_out = avcodec_parameters_alloc();
  76. if (!ctx->par_in || !ctx->par_out) {
  77. ret = AVERROR(ENOMEM);
  78. goto fail;
  79. }
  80. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  81. if (!ctx->internal) {
  82. ret = AVERROR(ENOMEM);
  83. goto fail;
  84. }
  85. ctx->internal->buffer_pkt = av_packet_alloc();
  86. if (!ctx->internal->buffer_pkt) {
  87. ret = AVERROR(ENOMEM);
  88. goto fail;
  89. }
  90. av_opt_set_defaults(ctx);
  91. /* allocate priv data and init private options */
  92. if (filter->priv_data_size) {
  93. ctx->priv_data = av_mallocz(filter->priv_data_size);
  94. if (!ctx->priv_data) {
  95. ret = AVERROR(ENOMEM);
  96. goto fail;
  97. }
  98. if (filter->priv_class) {
  99. *(const AVClass **)ctx->priv_data = filter->priv_class;
  100. av_opt_set_defaults(ctx->priv_data);
  101. }
  102. }
  103. *pctx = ctx;
  104. return 0;
  105. fail:
  106. av_bsf_free(&ctx);
  107. return ret;
  108. }
  109. int av_bsf_init(AVBSFContext *ctx)
  110. {
  111. int ret, i;
  112. /* check that the codec is supported */
  113. if (ctx->filter->codec_ids) {
  114. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
  115. if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
  116. break;
  117. if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
  118. const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
  119. av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
  120. "bitstream filter '%s'. Supported codecs are: ",
  121. desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
  122. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
  123. desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
  124. av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
  125. desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
  126. }
  127. av_log(ctx, AV_LOG_ERROR, "\n");
  128. return AVERROR(EINVAL);
  129. }
  130. }
  131. /* initialize output parameters to be the same as input
  132. * init below might overwrite that */
  133. ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
  134. if (ret < 0)
  135. return ret;
  136. ctx->time_base_out = ctx->time_base_in;
  137. if (ctx->filter->init) {
  138. ret = ctx->filter->init(ctx);
  139. if (ret < 0)
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  145. {
  146. if (!pkt) {
  147. ctx->internal->eof = 1;
  148. return 0;
  149. }
  150. av_assert0(pkt->data || pkt->side_data);
  151. if (ctx->internal->eof) {
  152. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  153. return AVERROR(EINVAL);
  154. }
  155. if (ctx->internal->buffer_pkt->data ||
  156. ctx->internal->buffer_pkt->side_data_elems)
  157. return AVERROR(EAGAIN);
  158. av_packet_move_ref(ctx->internal->buffer_pkt, pkt);
  159. return 0;
  160. }
  161. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  162. {
  163. return ctx->filter->filter(ctx, pkt);
  164. }
  165. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  166. {
  167. AVBSFInternal *in = ctx->internal;
  168. AVPacket *tmp_pkt;
  169. if (in->eof)
  170. return AVERROR_EOF;
  171. if (!ctx->internal->buffer_pkt->data &&
  172. !ctx->internal->buffer_pkt->side_data_elems)
  173. return AVERROR(EAGAIN);
  174. tmp_pkt = av_packet_alloc();
  175. if (!tmp_pkt)
  176. return AVERROR(ENOMEM);
  177. *pkt = ctx->internal->buffer_pkt;
  178. ctx->internal->buffer_pkt = tmp_pkt;
  179. return 0;
  180. }