Ticket #3014: JpegTest.cpp

File JpegTest.cpp, 2.0 KB (added by cyril, 13 years ago)
Line 
1// JpegTest.cpp : Defines the entry point for the console application.
2//
3
4#include "stdafx.h"
5
6extern "C" {
7# include "libavformat\avformat.h"
8# include "libavcodec\avcodec.h"
9# include "libavutil\avutil.h"
10# include "libavutil\frame.h"
11# include "libswscale\swscale.h"
12}
13
14#pragma comment( lib, "lib/avcodec" )
15#pragma comment( lib, "lib/avfilter" )
16#pragma comment( lib, "lib/avformat" )
17#pragma comment( lib, "lib/avutil" )
18#pragma comment( lib, "lib/swscale" )
19
20int _tmain(int argc, _TCHAR* argv[])
21{
22 av_register_all();
23
24 AVInputFormat *inputFormat = av_find_input_format("image2");
25
26 AVFormatContext *pFC = nullptr;
27 int res = avformat_open_input(&pFC, "F:\\test\\root_banner.jpg", inputFormat, nullptr);
28
29 res = avformat_find_stream_info(pFC, nullptr);
30
31 AVCodec *pCodec = nullptr;
32 int idx = av_find_best_stream(pFC, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, -1);
33
34 AVStream *pStream = pFC->streams[idx];
35 res = avcodec_open2(pStream->codec, pCodec, nullptr);
36
37 AVPacket packet;
38 av_read_frame(pFC, &packet);
39
40 int gotPicture;
41 AVFrame* pSrc = avcodec_alloc_frame();
42 avcodec_decode_video2(pStream->codec, pSrc, &gotPicture, &packet);
43
44 SwsContext *pSwsContext = nullptr;
45 pSwsContext = sws_getCachedContext(pSwsContext,
46 pSrc->width, pSrc->height,
47 static_cast<PixelFormat>(pSrc->format),
48 1, 60, PIX_FMT_BGRA,
49 SWS_BILINEAR, nullptr, nullptr, nullptr
50 );
51
52 AVFrame* pDest = avcodec_alloc_frame();
53 avpicture_alloc(reinterpret_cast<AVPicture *>(pDest), PIX_FMT_BGRA, 1, 60);
54 sws_scale(pSwsContext, pSrc->data, pSrc->linesize,
55 0, pSrc->height, pDest->data, pDest->linesize
56 );
57
58 av_free_packet(&packet);
59 // in debug mode: HEAP[JpegTest.exe]: Heap block at 00706758 modified at 00706874 past requested size of 114
60 avpicture_free(reinterpret_cast<AVPicture *>(pDest));
61 av_freep(&pDest);
62 av_freep(&pSrc);
63 avcodec_close(pStream->codec);
64 avformat_close_input(&pFC);
65
66 return 0;
67}
68