utils.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "internal.h"
  19. #include "libavutil/opt.h"
  20. #include "libavformat/avformat.h"
  21. #include "libavformat/demux.h"
  22. int ff_alloc_input_device_context(AVFormatContext **avctx, const AVInputFormat *iformat, const char *format)
  23. {
  24. AVFormatContext *s;
  25. int ret = 0;
  26. *avctx = NULL;
  27. if (!iformat && !format)
  28. return AVERROR(EINVAL);
  29. if (!(s = avformat_alloc_context()))
  30. return AVERROR(ENOMEM);
  31. if (!iformat)
  32. iformat = av_find_input_format(format);
  33. if (!iformat || !iformat->priv_class || !AV_IS_INPUT_DEVICE(iformat->priv_class->category)) {
  34. ret = AVERROR(EINVAL);
  35. goto error;
  36. }
  37. s->iformat = iformat;
  38. if (ffifmt(s->iformat)->priv_data_size > 0) {
  39. s->priv_data = av_mallocz(ffifmt(s->iformat)->priv_data_size);
  40. if (!s->priv_data) {
  41. ret = AVERROR(ENOMEM);
  42. goto error;
  43. }
  44. if (s->iformat->priv_class) {
  45. *(const AVClass**)s->priv_data= s->iformat->priv_class;
  46. av_opt_set_defaults(s->priv_data);
  47. }
  48. } else
  49. s->priv_data = NULL;
  50. *avctx = s;
  51. return 0;
  52. error:
  53. avformat_free_context(s);
  54. return ret;
  55. }