Ticket #2981: JpegTest.cpp

File JpegTest.cpp, 2.1 KB (added by cyril, 13 years ago)

Win32 console application

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 char *errbuf = new char[1000];
30 av_strerror(res, errbuf, 1000);
31 delete [] errbuf;
32
33 res = avformat_find_stream_info(pFC, nullptr);
34
35 AVCodec *pCodec = nullptr;
36 int idx = av_find_best_stream(pFC, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, -1);
37
38 AVStream *pStream = pFC->streams[idx];
39 res = avcodec_open2(pStream->codec, pCodec, nullptr);
40
41 AVPacket packet;
42 av_read_frame(pFC, &packet);
43
44 int gotPicture;
45 AVFrame* pSrc = avcodec_alloc_frame();
46 avcodec_decode_video2(pStream->codec, pSrc, &gotPicture, &packet);
47
48 SwsContext *pSwsContext = nullptr;
49 pSwsContext = sws_getCachedContext(pSwsContext,
50 pSrc->width, pSrc->height,
51 static_cast<PixelFormat>(pSrc->format),
52 1, 60, PIX_FMT_BGRA,
53 SWS_BILINEAR, nullptr, nullptr, nullptr
54 );
55
56 AVFrame* pDest = avcodec_alloc_frame();
57 avpicture_alloc(reinterpret_cast<AVPicture *>(pDest), PIX_FMT_BGRA, 1, 60);
58 sws_scale(pSwsContext, pSrc->data, pSrc->linesize,
59 0, pSrc->height, pDest->data, pDest->linesize
60 );
61
62 av_free_packet(&packet);
63 // in debug mode: HEAP[JpegTest.exe]: Heap block at 00706758 modified at 00706874 past requested size of 114
64 avpicture_free(reinterpret_cast<AVPicture *>(pDest));
65 av_freep(&pDest);
66 av_freep(&pSrc);
67 avcodec_close(pStream->codec);
68 avformat_close_input(&pFC);
69
70 return 0;
71}
72