Last active
May 11, 2016 00:02
-
-
Save neuro-sys/4a3897e115b4969b35277c44c85fd8d6 to your computer and use it in GitHub Desktop.
libav-test.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libavformat/avformat.h> | |
#include <libavcodec/avcodec.h> | |
int main(int argc, char *argv[]) | |
{ | |
AVFormatContext *fmt_ctx = NULL; | |
AVCodecContext *dec_ctx = NULL; | |
AVCodec *dec = NULL; | |
AVFrame *frame = NULL; | |
AVPacket packet; | |
int ret; | |
int video_stream_index; | |
av_register_all(); | |
if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)) < 0) { | |
fprintf(stderr, "Failed to open file %s\n", argv[1]); | |
return -1; | |
} | |
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { | |
fprintf(stderr, "failed to find stream info %s\n", argv[1]); | |
return -1; | |
} | |
if ((ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0)) < 0) { | |
fprintf(stderr, "No streams were found\n"); | |
return -1; | |
} | |
video_stream_index = ret; | |
dec_ctx = fmt_ctx->streams[video_stream_index]->codec; | |
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { | |
fprintf(stderr, "Could not open codec\n"); | |
return -1; | |
} | |
if (!(frame = av_frame_alloc())) { | |
fprintf(stderr, "Could not alloc frame\n"); | |
return -1; | |
} | |
while(1) { | |
if ((ret = av_read_frame(fmt_ctx, &packet)) < 0) { | |
fprintf(stderr, "Could not read frame\n"); | |
break; | |
} | |
if (packet.stream_index == video_stream_index) { | |
int got_frame = 0; | |
if ((ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet)) < 0) { | |
fprintf(stderr, "Could not decode frame\n"); | |
break; | |
} | |
if (got_frame) { | |
frame->pts = av_frame_get_best_effort_timestamp(frame); | |
av_frame_unref(frame); | |
} | |
} | |
av_packet_unref(&packet); | |
} | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libavformat/avformat.h> | |
#include <libavcodec/avcodec.h> | |
#include <libavutil/opt.h> | |
#include <libavutil/imgutils.h> | |
int main(int argc, char *argv[]) | |
{ | |
AVFormatContext *fmt_ctx = NULL; | |
AVCodecContext *dec_ctx = NULL; | |
AVCodec *dec = NULL; | |
AVFrame *frame = NULL; | |
int ret; | |
int video_stream_index; | |
int width, height; | |
enum AVPixelFormat pix_fmt; | |
uint8_t *video_dst_data[4] = { NULL }; | |
int video_dst_linesize[4] = { 0 }; | |
int video_dst_bufsize; | |
av_register_all(); | |
if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)) < 0) { | |
fprintf(stderr, "Failed to open file %s\n", argv[1]); | |
return -1; | |
} | |
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { | |
fprintf(stderr, "failed to find stream info %s\n", argv[1]); | |
return -1; | |
} | |
if ((ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0)) < 0) { | |
fprintf(stderr, "No streams were found\n"); | |
return -1; | |
} | |
video_stream_index = ret; | |
dec_ctx = fmt_ctx->streams[video_stream_index]->codec; | |
width = dec_ctx->width; | |
height = dec_ctx->height; | |
pix_fmt = dec_ctx->pix_fmt; | |
if ((ret = av_image_alloc(video_dst_data, video_dst_linesize, width, height, pix_fmt, 1)) < 0) { | |
fprintf(stderr, "Could not allocate image data buffer\n"); | |
return -1; | |
} | |
video_dst_bufsize = ret; | |
av_dump_format(fmt_ctx, 0, argv[1], 0); | |
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { | |
fprintf(stderr, "Could not open codec\n"); | |
return -1; | |
} | |
if (!(frame = av_frame_alloc())) { | |
fprintf(stderr, "Could not alloc frame\n"); | |
return -1; | |
} | |
while(1) { | |
AVPacket packet; | |
if ((ret = av_read_frame(fmt_ctx, &packet)) < 0) { | |
fprintf(stderr, "Could not read frame\n"); | |
break; | |
} | |
if (packet.stream_index == video_stream_index) { | |
int got_frame = 0; | |
if ((ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet)) < 0) { | |
fprintf(stderr, "Could not decode frame\n"); | |
break; | |
} | |
if (got_frame) { | |
frame->pts = av_frame_get_best_effort_timestamp(frame); | |
// Do something with frame | |
av_frame_unref(frame); | |
} | |
} | |
av_packet_unref(&packet); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment