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.

220 lines
5.7KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  23. #include "bsf.h"
  24. struct AVBSFInternal {
  25. AVPacket *buffer_pkt;
  26. int eof;
  27. };
  28. void av_bsf_free(AVBSFContext **pctx)
  29. {
  30. AVBSFContext *ctx;
  31. if (!pctx || !*pctx)
  32. return;
  33. ctx = *pctx;
  34. if (ctx->filter->close)
  35. ctx->filter->close(ctx);
  36. if (ctx->filter->priv_class && ctx->priv_data)
  37. av_opt_free(ctx->priv_data);
  38. av_opt_free(ctx);
  39. av_packet_free(&ctx->internal->buffer_pkt);
  40. av_freep(&ctx->internal);
  41. av_freep(&ctx->priv_data);
  42. avcodec_parameters_free(&ctx->par_in);
  43. avcodec_parameters_free(&ctx->par_out);
  44. av_freep(pctx);
  45. }
  46. static void *bsf_child_next(void *obj, void *prev)
  47. {
  48. AVBSFContext *ctx = obj;
  49. if (!prev && ctx->filter->priv_class)
  50. return ctx->priv_data;
  51. return NULL;
  52. }
  53. static const AVClass bsf_class = {
  54. .class_name = "AVBSFContext",
  55. .item_name = av_default_item_name,
  56. .version = LIBAVUTIL_VERSION_INT,
  57. .child_next = bsf_child_next,
  58. .child_class_next = ff_bsf_child_class_next,
  59. };
  60. const AVClass *av_bsf_get_class(void)
  61. {
  62. return &bsf_class;
  63. }
  64. int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
  65. {
  66. AVBSFContext *ctx;
  67. int ret;
  68. ctx = av_mallocz(sizeof(*ctx));
  69. if (!ctx)
  70. return AVERROR(ENOMEM);
  71. ctx->av_class = &bsf_class;
  72. ctx->filter = filter;
  73. ctx->par_in = avcodec_parameters_alloc();
  74. ctx->par_out = avcodec_parameters_alloc();
  75. if (!ctx->par_in || !ctx->par_out) {
  76. ret = AVERROR(ENOMEM);
  77. goto fail;
  78. }
  79. ctx->internal = av_mallocz(sizeof(*ctx->internal));
  80. if (!ctx->internal) {
  81. ret = AVERROR(ENOMEM);
  82. goto fail;
  83. }
  84. ctx->internal->buffer_pkt = av_packet_alloc();
  85. if (!ctx->internal->buffer_pkt) {
  86. ret = AVERROR(ENOMEM);
  87. goto fail;
  88. }
  89. av_opt_set_defaults(ctx);
  90. /* allocate priv data and init private options */
  91. if (filter->priv_data_size) {
  92. ctx->priv_data = av_mallocz(filter->priv_data_size);
  93. if (!ctx->priv_data) {
  94. ret = AVERROR(ENOMEM);
  95. goto fail;
  96. }
  97. if (filter->priv_class) {
  98. *(const AVClass **)ctx->priv_data = filter->priv_class;
  99. av_opt_set_defaults(ctx->priv_data);
  100. }
  101. }
  102. *pctx = ctx;
  103. return 0;
  104. fail:
  105. av_bsf_free(&ctx);
  106. return ret;
  107. }
  108. int av_bsf_init(AVBSFContext *ctx)
  109. {
  110. int ret, i;
  111. /* check that the codec is supported */
  112. if (ctx->filter->codec_ids) {
  113. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++)
  114. if (ctx->par_in->codec_id == ctx->filter->codec_ids[i])
  115. break;
  116. if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) {
  117. const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id);
  118. av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the "
  119. "bitstream filter '%s'. Supported codecs are: ",
  120. desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name);
  121. for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
  122. desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]);
  123. av_log(ctx, AV_LOG_ERROR, "%s (%d) ",
  124. desc ? desc->name : "unknown", ctx->filter->codec_ids[i]);
  125. }
  126. av_log(ctx, AV_LOG_ERROR, "\n");
  127. return AVERROR(EINVAL);
  128. }
  129. }
  130. /* initialize output parameters to be the same as input
  131. * init below might overwrite that */
  132. ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in);
  133. if (ret < 0)
  134. return ret;
  135. ctx->time_base_out = ctx->time_base_in;
  136. if (ctx->filter->init) {
  137. ret = ctx->filter->init(ctx);
  138. if (ret < 0)
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
  144. {
  145. if (!pkt || !pkt->data) {
  146. ctx->internal->eof = 1;
  147. return 0;
  148. }
  149. if (ctx->internal->eof) {
  150. av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n");
  151. return AVERROR(EINVAL);
  152. }
  153. if (ctx->internal->buffer_pkt->data ||
  154. ctx->internal->buffer_pkt->side_data_elems)
  155. return AVERROR(EAGAIN);
  156. av_packet_move_ref(ctx->internal->buffer_pkt, pkt);
  157. return 0;
  158. }
  159. int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
  160. {
  161. return ctx->filter->filter(ctx, pkt);
  162. }
  163. int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
  164. {
  165. AVBSFInternal *in = ctx->internal;
  166. AVPacket *tmp_pkt;
  167. if (in->eof)
  168. return AVERROR_EOF;
  169. if (!ctx->internal->buffer_pkt->data &&
  170. !ctx->internal->buffer_pkt->side_data_elems)
  171. return AVERROR(EAGAIN);
  172. tmp_pkt = av_packet_alloc();
  173. if (!tmp_pkt)
  174. return AVERROR(ENOMEM);
  175. *pkt = ctx->internal->buffer_pkt;
  176. ctx->internal->buffer_pkt = tmp_pkt;
  177. return 0;
  178. }