//.h#ifndef _JPG_PROCESS_H
#define _JPG_PROCESS_H
#if !defined PRId64 || PRI_MACROS_BROKEN
# undef PRId64
# define PRId64 "lld"
#endif
#ifndef INT64_C
#define INT64_C
#define UINT64_C
#endif
#include
namespace ffmpeg{
extern "C"
{
#include
#include
#include
#include
#include
#include
}
}
using namespace ffmpeg;
struct JpgProcessParam
{
AVFormatContext *fmt_ctx;
AVCodecContext *video_dec_ctx, *audio_dec_ctx;
AVStream *video_stream, *audio_stream;
char *in_filename;
char *out_filename;
char *audio_dst_filename;
FILE *video_dst_file;
FILE *audio_dst_file;
uint8_t *video_dst_data[4];
int video_dst_linesize[4];
int video_dst_bufsize;
int video_stream_idx, audio_stream_idx;
AVFrame *frame;
AVPacket pkt;
int video_frame_count;
int audio_frame_count;
};
class JpgProcess
{
private:
int decode_packet(int *got_frame, int cached);
int open_codec_context(int *stream_idx, AVFormatContext *fmt_ctx, enum AVMediaType type);
int get_format_from_sample_fmt(const char **fmt, enum AVSampleFormat sample_fmt);
int main_jpg();
JpgProcessParam jpgProcessParam;
public:
bool jpgprocess(char *src_filename, char *dst_filename);
};
#endif
//.cpp
#include "jpgprocess.h"
bool JpgProcess::jpgprocess(char *src_filename, char *dst_filename)
{
if ((NULL == src_filename) || (NULL == dst_filename))
return false;
memset(&jpgProcessParam, 0x00, sizeof(jpgProcessParam));
jpgProcessParam.in_filename = src_filename;
jpgProcessParam.out_filename = dst_filename;
main_jpg();
return true;
}
int JpgProcess::main_jpg()
{
int ret = 0, got_frame;
/* register all formats and codecs */
av_register_all();
/* open input file, and allocate format context */
if (avformat_open_input(&jpgProcessParam.fmt_ctx, jpgProcessParam.in_filename, NULL, NULL) < 0) {
fprintf(stderr, "Could not open source file %s\n", jpgProcessParam.in_filename);
exit(1);
}
/* retrieve stream information */
if (avformat_find_stream_info(jpgProcessParam.fmt_ctx, NULL) < 0) {
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
if (open_codec_context(&jpgProcessParam.video_stream_idx, jpgProcessParam.fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) {
jpgProcessParam.video_stream = jpgProcessParam.fmt_ctx->streams[jpgProcessParam.video_stream_idx];
jpgProcessParam.video_dec_ctx = jpgProcessParam.video_stream->codec;
jpgProcessParam.video_dst_file = fopen(jpgProcessParam.out_filename, "wb");
if (!j