Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <time.h>
- #include <inttypes.h>
- #include <stdint.h>
- using namespace std;
- extern "C" {
- #include <libavcodec/avcodec.h>
- #include <libavformat/avformat.h>
- #include <libavutil/avutil.h>
- }
- int totalFrames = 0;
- int frameLocation = 0;
- int Scan(const char *inputfile)
- {
- totalFrames = 0;
- frameLocation = 0;
- AVFormatContext *pFormatCtx;
- unsigned int i, videoStream;
- AVCodecContext *pCodecCtx;
- AVCodec *pCodec;
- AVFrame *pFrame;
- AVFrame *pFrameYUV;
- int numBytes;
- uint8_t *buffer;
- clock_t start = clock();
- // Register all formats and codecs
- av_register_all();
- // Open video file
- pFormatCtx = avformat_alloc_context();
- if(avformat_open_input(&pFormatCtx, inputfile, NULL, NULL)!=0)
- {
- return -1; // Couldn't open file
- }
- // Retrieve stream information
- if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
- {
- return -2; // Couldn't find stream information
- }
- // Dump information about file onto standard error
- av_dump_format(pFormatCtx, 0, inputfile, false);
- // Find the first video stream
- videoStream=-1;
- for(i=0; i<pFormatCtx->nb_streams; i++)
- {
- if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
- {
- videoStream=i;
- break;
- }
- }
- if(videoStream==-1)
- {
- return -3; // Didn't find a video stream
- }
- // Get a pointer to the codec context for the video stream
- pCodecCtx = pFormatCtx->streams[videoStream]->codec;
- // get duration
- double duration = double(pFormatCtx->duration) / AV_TIME_BASE;
- cout << "Video duration: " << duration << std::endl;
- // Find the decoder for the video stream
- pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
- if(pCodec==NULL)
- {
- return -4; // Codec not found
- }
- // Inform the codec that we can handle truncated bitstreams -- i.e.,
- // bitstreams where frame boundaries can fall in the middle of packets
- if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
- pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
- // Open codec
- if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
- {
- return -5; // Could not open codec
- }
- std::cout << "Using codec: " << pCodec->name << std::endl;
- // Allocate video frame
- pFrame=avcodec_alloc_frame();
- // Allocate an AVFrame structure
- pFrameYUV=avcodec_alloc_frame();
- if(pFrameYUV==NULL)
- {
- return -6;
- }
- // Determine required buffer size and allocate buffer
- // originally: PIX_FMT_RGB24
- numBytes=avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
- buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
- // Assign appropriate parts of buffer to image planes in pFrameRGB
- // originally: PIX_FMT_RGB24
- avpicture_fill((AVPicture *)pFrameYUV, buffer, PIX_FMT_YUV420P,
- pCodecCtx->width, pCodecCtx->height);
- totalFrames = pFormatCtx->streams[videoStream]->nb_frames;
- //printf("Frames: %d\n", totalFrames);
- AVPacket packet;
- int isFrameFinished = 0;
- int pixelScanCount = 0;
- std::cout << "videoWidth = " << pCodecCtx->width << std::endl;
- std::cout << "videoHeight = " << pCodecCtx->height << std::endl;
- while (av_read_frame(pFormatCtx, &packet) >= 0)
- {
- std::cout << "have read frame" << std::endl;
- // Is this a packet from the video stream?
- if (packet.stream_index == videoStream)
- {
- avcodec_decode_video2(pCodecCtx, pFrame, &isFrameFinished, &packet);
- if (isFrameFinished) {
- cout << "got pic" << endl;
- }
- else
- {
- cout << "NOT got pic" << endl;
- }
- av_free_packet(&packet);
- }
- }
- delete [] buffer;
- av_free(pFrameYUV);
- // Free the YUV frame
- av_free(pFrame);
- // Close the codec
- avcodec_close(pCodecCtx);
- // Close the video file
- av_close_input_file(pFormatCtx);
- double elapsed = ((double)clock() - start) / CLOCKS_PER_SEC;
- printf("Time elapsed: %f\n", elapsed);
- printf("Duration: %f\n", duration);
- printf("Scan speed: %fx", duration / elapsed);
- //std::cin >> i; // keep window open
- return pixelScanCount;
- }
- int main() {
- Scan("00793.MTS");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement