avio_reading.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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
  24. * libavformat AVIOContext API example.
  25. *
  26. * Make libavformat demuxer access media content through a custom
  27. * AVIOContext read callback.
  28. * @example avio_reading.c
  29. */
  30. #include <libavcodec/avcodec.h>
  31. #include <libavformat/avformat.h>
  32. #include <libavformat/avio.h>
  33. #include <libavutil/file.h>
  34. struct buffer_data {
  35. uint8_t *ptr;
  36. size_t size; ///< size left in the buffer
  37. };
  38. static int read_packet(void *opaque, uint8_t *buf, int buf_size)
  39. {
  40. struct buffer_data *bd = (struct buffer_data *)opaque;
  41. buf_size = FFMIN(buf_size, bd->size);
  42. printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
  43. /* copy internal buffer data to buf */
  44. memcpy(buf, bd->ptr, buf_size);
  45. bd->ptr += buf_size;
  46. bd->size -= buf_size;
  47. return buf_size;
  48. }
  49. int main(int argc, char *argv[])
  50. {
  51. AVFormatContext *fmt_ctx = NULL;
  52. AVIOContext *avio_ctx = NULL;
  53. uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
  54. size_t buffer_size, avio_ctx_buffer_size = 4096;
  55. char *input_filename = NULL;
  56. int ret = 0;
  57. struct buffer_data bd = { 0 };
  58. if (argc != 2) {
  59. fprintf(stderr, "usage: %s input_file\n"
  60. "API example program to show how to read from a custom buffer "
  61. "accessed through AVIOContext.\n", argv[0]);
  62. return 1;
  63. }
  64. input_filename = argv[1];
  65. /* register codecs and formats and other lavf/lavc components*/
  66. av_register_all();
  67. /* slurp file content into buffer */
  68. ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL);
  69. if (ret < 0)
  70. goto end;
  71. /* fill opaque structure used by the AVIOContext read callback */
  72. bd.ptr = buffer;
  73. bd.size = buffer_size;
  74. if (!(fmt_ctx = avformat_alloc_context())) {
  75. ret = AVERROR(ENOMEM);
  76. goto end;
  77. }
  78. avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
  79. if (!avio_ctx_buffer) {
  80. ret = AVERROR(ENOMEM);
  81. goto end;
  82. }
  83. avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size,
  84. 0, &bd, &read_packet, NULL, NULL);
  85. if (!avio_ctx) {
  86. ret = AVERROR(ENOMEM);
  87. goto end;
  88. }
  89. fmt_ctx->pb = avio_ctx;
  90. ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL);
  91. if (ret < 0) {
  92. fprintf(stderr, "Could not open input\n");
  93. goto end;
  94. }
  95. ret = avformat_find_stream_info(fmt_ctx, NULL);
  96. if (ret < 0) {
  97. fprintf(stderr, "Could not find stream information\n");
  98. goto end;
  99. }
  100. av_dump_format(fmt_ctx, 0, input_filename, 0);
  101. end:
  102. avformat_close_input(&fmt_ctx);
  103. /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
  104. if (avio_ctx) {
  105. av_freep(&avio_ctx->buffer);
  106. av_freep(&avio_ctx);
  107. }
  108. av_file_unmap(buffer, buffer_size);
  109. if (ret < 0) {
  110. fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
  111. return 1;
  112. }
  113. return 0;
  114. }