Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. int main(int argc, char **argv)
  2. {
  3. avcodec_register_all();
  4.  
  5. if (argc != 2)
  6. return 1;
  7.  
  8. try {
  9. AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
  10. if (!codec)
  11. throw std::runtime_error{ "no H264 decoder" };
  12.  
  13. AVCodecContext_uptr avctx_uptr{ avcodec_alloc_context3(codec) };
  14. AVCodecContext *avctx = avctx_uptr.get();
  15. if (!avctx_uptr)
  16. throw std::runtime_error{ "error allocating H264 context" };
  17. if (avcodec_open2(avctx, codec, nullptr) < 0)
  18. throw std::runtime_error{ "error initializing H264 context" };
  19.  
  20. AVCodecParserContext_uptr avparser_uptr{ av_parser_init(AV_CODEC_ID_H264) };
  21. AVCodecParserContext *avparser = avparser_uptr.get();
  22. if (!avparser_uptr)
  23. throw std::runtime_error{ "error initializing H264 parser" };
  24.  
  25. const MemoryMappedFile file{ argv[1], MemoryMappedFile::READ_TAG };
  26. const uint8_t *data = static_cast<const uint8_t *>(file.read_ptr());
  27. size_t size = file.size();
  28. size_t offset = 0;
  29. int n = 0;
  30.  
  31. while (size) {
  32. uint8_t *outbuf;
  33. int outbuf_size;
  34. int len = av_parser_parse2(avparser,
  35. avctx,
  36. &outbuf,
  37. &outbuf_size,
  38. data,
  39. size,
  40. AV_NOPTS_VALUE,
  41. AV_NOPTS_VALUE,
  42. 0);
  43. data += len;
  44. size -= len;
  45.  
  46. if (outbuf_size) {
  47. std::cout << n++ << ',' << offset << ',' << outbuf_size << '\n';
  48. offset += outbuf_size;
  49. }
  50. }
  51. } catch (const std::exception &e) {
  52. std::cerr << e.what() << '\n';
  53. return 2;
  54. }
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement