utils.c 1.8 KB

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