Advertisement
Guest User

Untitled

a guest
Feb 7th, 2012
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <time.h>
  4. #include <inttypes.h>
  5. #include <stdint.h>
  6.  
  7. using namespace std;
  8.  
  9. extern "C" {
  10. #include <libavcodec/avcodec.h>
  11. #include <libavformat/avformat.h>
  12. #include <libavutil/avutil.h>
  13. }
  14.  
  15. int totalFrames = 0;
  16. int frameLocation = 0;
  17.  
  18. int Scan(const char *inputfile)
  19. {
  20.  
  21. totalFrames = 0;
  22. frameLocation = 0;
  23.  
  24. AVFormatContext *pFormatCtx;
  25. unsigned int i, videoStream;
  26. AVCodecContext *pCodecCtx;
  27. AVCodec *pCodec;
  28. AVFrame *pFrame;
  29. AVFrame *pFrameYUV;
  30. int numBytes;
  31. uint8_t *buffer;
  32.  
  33. clock_t start = clock();
  34.  
  35. // Register all formats and codecs
  36. av_register_all();
  37.  
  38. // Open video file
  39. pFormatCtx = avformat_alloc_context();
  40. if(avformat_open_input(&pFormatCtx, inputfile, NULL, NULL)!=0)
  41. {
  42. return -1; // Couldn't open file
  43. }
  44.  
  45. // Retrieve stream information
  46. if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
  47. {
  48. return -2; // Couldn't find stream information
  49. }
  50.  
  51. // Dump information about file onto standard error
  52. av_dump_format(pFormatCtx, 0, inputfile, false);
  53.  
  54. // Find the first video stream
  55. videoStream=-1;
  56. for(i=0; i<pFormatCtx->nb_streams; i++)
  57. {
  58. if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
  59. {
  60. videoStream=i;
  61. break;
  62. }
  63. }
  64. if(videoStream==-1)
  65. {
  66. return -3; // Didn't find a video stream
  67. }
  68.  
  69. // Get a pointer to the codec context for the video stream
  70. pCodecCtx = pFormatCtx->streams[videoStream]->codec;
  71.  
  72. // get duration
  73. double duration = double(pFormatCtx->duration) / AV_TIME_BASE;
  74. cout << "Video duration: " << duration << std::endl;
  75.  
  76. // Find the decoder for the video stream
  77. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  78. if(pCodec==NULL)
  79. {
  80. return -4; // Codec not found
  81. }
  82.  
  83. // Inform the codec that we can handle truncated bitstreams -- i.e.,
  84. // bitstreams where frame boundaries can fall in the middle of packets
  85. if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
  86. pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
  87.  
  88. // Open codec
  89. if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
  90. {
  91. return -5; // Could not open codec
  92. }
  93.  
  94. std::cout << "Using codec: " << pCodec->name << std::endl;
  95.  
  96. // Allocate video frame
  97. pFrame=avcodec_alloc_frame();
  98.  
  99. // Allocate an AVFrame structure
  100. pFrameYUV=avcodec_alloc_frame();
  101. if(pFrameYUV==NULL)
  102. {
  103. return -6;
  104. }
  105.  
  106. // Determine required buffer size and allocate buffer
  107. // originally: PIX_FMT_RGB24
  108. numBytes=avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
  109. buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  110.  
  111. // Assign appropriate parts of buffer to image planes in pFrameRGB
  112. // originally: PIX_FMT_RGB24
  113. avpicture_fill((AVPicture *)pFrameYUV, buffer, PIX_FMT_YUV420P,
  114. pCodecCtx->width, pCodecCtx->height);
  115.  
  116. totalFrames = pFormatCtx->streams[videoStream]->nb_frames;
  117. //printf("Frames: %d\n", totalFrames);
  118.  
  119. AVPacket packet;
  120. int isFrameFinished = 0;
  121.  
  122. int pixelScanCount = 0;
  123.  
  124. std::cout << "videoWidth = " << pCodecCtx->width << std::endl;
  125. std::cout << "videoHeight = " << pCodecCtx->height << std::endl;
  126.  
  127. while (av_read_frame(pFormatCtx, &packet) >= 0)
  128. {
  129. std::cout << "have read frame" << std::endl;
  130.  
  131. // Is this a packet from the video stream?
  132. if (packet.stream_index == videoStream)
  133. {
  134. avcodec_decode_video2(pCodecCtx, pFrame, &isFrameFinished, &packet);
  135. if (isFrameFinished) {
  136. cout << "got pic" << endl;
  137. }
  138. else
  139. {
  140. cout << "NOT got pic" << endl;
  141. }
  142. av_free_packet(&packet);
  143. }
  144. }
  145.  
  146. delete [] buffer;
  147. av_free(pFrameYUV);
  148.  
  149. // Free the YUV frame
  150. av_free(pFrame);
  151.  
  152. // Close the codec
  153. avcodec_close(pCodecCtx);
  154.  
  155. // Close the video file
  156. av_close_input_file(pFormatCtx);
  157.  
  158.  
  159. double elapsed = ((double)clock() - start) / CLOCKS_PER_SEC;
  160. printf("Time elapsed: %f\n", elapsed);
  161. printf("Duration: %f\n", duration);
  162. printf("Scan speed: %fx", duration / elapsed);
  163. //std::cin >> i; // keep window open
  164.  
  165. return pixelScanCount;
  166.  
  167. }
  168.  
  169. int main() {
  170. Scan("00793.MTS");
  171. return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement