Advertisement
Guest User

Untitled

a guest
Jan 29th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <libavcodec/avcodec.h>
  2. #include <libavformat/avformat.h>
  3. #include <libavformat/avio.h>
  4. #include <libavutil/file.h>
  5.  
  6. struct buffer_data {
  7. uint8_t *ptr;
  8. size_t size; ///< size left in the buffer
  9. };
  10.  
  11. static int read_packet(void *opaque, uint8_t *buf, int buf_size)
  12. {
  13. struct buffer_data *bd = (struct buffer_data *)opaque;
  14. buf_size = FFMIN(buf_size, bd->size);
  15. printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
  16. /* copy internal buffer data to buf */
  17. memcpy(buf, bd->ptr, buf_size);
  18. bd->ptr += buf_size;
  19. bd->size -= buf_size;
  20. return buf_size;
  21. }
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25. AVFormatContext *fmt_ctx = NULL;
  26. AVIOContext *avio_ctx = NULL;
  27. AVInputFormat *iformat = NULL;
  28. uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
  29. size_t buffer_size, avio_ctx_buffer_size = 4096;
  30. char *input_filename = NULL;
  31. int ret = 0;
  32. struct buffer_data bd = { 0 };
  33. if (argc != 2) {
  34. fprintf(stderr, "usage: %s input_file\n"
  35. "API example program to show how to read from a custom buffer "
  36. "accessed through AVIOContext.\n", argv[0]);
  37. return 1;
  38. }
  39. input_filename = argv[1];
  40.  
  41. /* register codecs and formats and other lavf/lavc components*/
  42. av_register_all();
  43.  
  44. /* slurp file content into buffer */
  45. ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL);
  46. if (ret < 0)
  47. goto end;
  48.  
  49. /* fill opaque structure used by the AVIOContext read callback */
  50. bd.ptr = buffer;
  51. bd.size = buffer_size;
  52. if (!(fmt_ctx = avformat_alloc_context())) {
  53. ret = AVERROR(ENOMEM);
  54. goto end;
  55. }
  56.  
  57. fmt_ctx->flags |= AVFMT_FLAG_NONBLOCK;
  58.  
  59. avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
  60. if (!avio_ctx_buffer) {
  61. ret = AVERROR(ENOMEM);
  62. goto end;
  63. }
  64.  
  65. avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size,
  66. 0, &bd, &read_packet, NULL, NULL);
  67. if (!avio_ctx) {
  68. ret = AVERROR(ENOMEM);
  69. goto end;
  70. }
  71.  
  72. fmt_ctx->pb = avio_ctx;
  73.  
  74. avio_ctx->max_packet_size = 6144;
  75.  
  76. ret = av_probe_input_buffer(avio_ctx, &iformat, input_filename, NULL, 0, 0);
  77. if (!iformat) {
  78. fprintf(stderr, "Failed to probe.\n");
  79. goto end;
  80. }
  81.  
  82. ret = avformat_open_input(&fmt_ctx, input_filename, iformat, NULL);
  83. if (ret < 0) {
  84. fprintf(stderr, "Could not open input\n");
  85. goto end;
  86. }
  87.  
  88. fmt_ctx->max_analyze_duration = 500000;
  89.  
  90. ret = avformat_find_stream_info(fmt_ctx, NULL);
  91. if (ret < 0) {
  92. fprintf(stderr, "Could not find stream information\n");
  93. goto end;
  94. }
  95. av_dump_format(fmt_ctx, 0, input_filename, 0);
  96. end:
  97. avformat_close_input(&fmt_ctx);
  98. /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
  99. if (avio_ctx) {
  100. av_freep(&avio_ctx->buffer);
  101. av_freep(&avio_ctx);
  102. }
  103. av_file_unmap(buffer, buffer_size);
  104. if (ret < 0) {
  105. fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
  106. return 1;
  107. }
  108. return 0;
  109. }
  110.  
  111. [mov,mp4,m4a,3gp,3g2,mj2 @ 0xa18820] stream 0, offset 0x24: partial file
  112. [mov,mp4,m4a,3gp,3g2,mj2 @ 0xa18820] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 1280x720, 11555 kb/s): unspecified pixel format
  113. Consider increasing the value for the 'analyzeduration' and 'probesize' options
  114. Could not find stream information
  115. Error occurred: Invalid data found when processing input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement