Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void decodeVideoToFrames(const char *filePath){
- AVCodec *codec;
- AVCodecContext *codecCtx= NULL;
- AVFormatContext *formatCtx = NULL;
- AVFrame *frame;
- int videoStreamIdx;
- AVPacket pkt;
- int frameFinished;
- avcodec_register_all();
- av_register_all();
- formatCtx = avformat_alloc_context();
- frame = av_frame_alloc();
- avformat_open_input(&formatCtx, filePath, 0, NULL);
- avformat_find_stream_info(formatCtx, NULL);
- videoStreamIdx = -1;
- //the for loop exit condition is wrong, not sure what the right one is yet.
- for(int i = 0; -1 == videoStreamIdx; i++){
- if(AVMEDIA_TYPE_VIDEO == formatCtx->streams[i]->codec->codec_type){
- videoStreamIdx = i;
- break;
- }
- }
- codecCtx = formatCtx->streams[videoStreamIdx]->codec;
- codec = avcodec_find_decoder(codecCtx->codec_id);
- avcodec_open2(codecCtx, codec, NULL);
- struct SwsContext* convertCtx;
- //This can be improved significantly
- convertCtx = sws_getContext(codecCtx->width, codecCtx->height, codecCtx->pix_fmt,
- 32, 18, AV_PIX_FMT_RGB24, 0, 0, 0, 0);
- while(av_read_frame(formatCtx, &pkt) >= 0){
- if(pkt.stream_index == videoStreamIdx){
- avcodec_decode_video2(codecCtx, frame, &frameFinished, &pkt);
- if(frameFinished){
- size_t allocSize = av_image_get_buffer_size(AV_PIX_FMT_RGB24, 32, 18, 3);
- uint8_t *data = (uint8_t*) malloc(allocSize);
- int outLinesize = 32 * 3;
- sws_scale(convertCtx, frame->data, frame->linesize, 0, 144,
- &data, &outLinesize);
- FramesIn.push(data);
- }
- }
- av_packet_unref(&pkt);
- }
- sws_freeContext(convertCtx);
- av_free(frame);
- avcodec_close(codecCtx);
- avformat_close_input(&formatCtx);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement