avio_read_callback.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2014 Stefano Sabatini
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file libavformat AVIOContext read callback API usage example
  24. * @example avio_read_callback.c
  25. *
  26. * Make libavformat demuxer access media content through a custom
  27. * AVIOContext read callback.
  28. */
  29. #include <libavcodec/avcodec.h>
  30. #include <libavformat/avformat.h>
  31. #include <libavformat/avio.h>
  32. #include <libavutil/file.h>
  33. struct buffer_data {
  34. uint8_t *ptr;
  35. size_t size; ///< size left in the buffer
  36. };
  37. static int read_packet(void *opaque, uint8_t *buf, int buf_size)
  38. {
  39. struct buffer_data *bd = (struct buffer_data *)opaque;
  40. buf_size = FFMIN(buf_size, bd->size);
  41. if (!buf_size)
  42. return AVERROR_EOF;
  43. printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
  44. /* copy internal buffer data to buf */
  45. memcpy(buf, bd->ptr, buf_size);
  46. bd->ptr += buf_size;
  47. bd->size -= buf_size;
  48. return buf_size;
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. AVFormatContext *fmt_ctx = NULL;
  53. AVIOContext *avio_ctx = NULL;
  54. uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
  55. size_t buffer_size, avio_ctx_buffer_size = 4096;
  56. char *input_filename = NULL;
  57. int ret = 0;
  58. struct buffer_data bd = { 0 };
  59. if (argc != 2) {
  60. fprintf(stderr, "usage: %s input_file\n"
  61. "API example program to show how to read from a custom buffer "
  62. "accessed through AVIOContext.\n", argv[0]);
  63. return 1;
  64. }
  65. input_filename = argv[1];
  66. /* slurp file content into buffer */
  67. ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL);
  68. if (ret < 0)
  69. goto end;
  70. /* fill opaque structure used by the AVIOContext read callback */
  71. bd.ptr = buffer;
  72. bd.size = buffer_size;
  73. if (!(fmt_ctx = avformat_alloc_context())) {
  74. ret = AVERROR(ENOMEM);
  75. goto end;
  76. }
  77. avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
  78. if (!avio_ctx_buffer) {
  79. ret = AVERROR(ENOMEM);
  80. goto end;
  81. }
  82. avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size,
  83. 0, &bd, &read_packet, NULL, NULL);
  84. if (!avio_ctx) {
  85. ret = AVERROR(ENOMEM);
  86. goto end;
  87. }
  88. fmt_ctx->pb = avio_ctx;
  89. ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL);
  90. if (ret < 0) {
  91. fprintf(stderr, "Could not open input\n");
  92. goto end;
  93. }
  94. ret = avformat_find_stream_info(fmt_ctx, NULL);
  95. if (ret < 0) {
  96. fprintf(stderr, "Could not find stream information\n");
  97. goto end;
  98. }
  99. av_dump_format(fmt_ctx, 0, input_filename, 0);
  100. end:
  101. avformat_close_input(&fmt_ctx);
  102. /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
  103. if (avio_ctx)
  104. av_freep(&avio_ctx->buffer);
  105. avio_context_free(&avio_ctx);
  106. av_file_unmap(buffer, buffer_size);
  107. if (ret < 0) {
  108. fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
  109. return 1;
  110. }
  111. return 0;
  112. }