// JpegTest.cpp : Defines the entry point for the console application.
//

#include "config.h"
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#if HAVE_ISATTY
#if HAVE_IO_H
#include <io.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#endif
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/dict.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
#include "libavutil/avstring.h"
#include "libavutil/libm.h"
#include "libavutil/imgutils.h"
#include "libavutil/timestamp.h"
#include "libavutil/bprint.h"
#include "libavutil/time.h"
#include "libavformat/os_support.h"

#include "libavformat/ffm.h" // not public API

# include "libavfilter/avcodec.h"
# include "libavfilter/avfilter.h"
# include "libavfilter/buffersrc.h"
# include "libavfilter/buffersink.h"

#if HAVE_SYS_RESOURCE_H
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#elif HAVE_GETPROCESSTIMES
#include <windows.h>
#endif
#if HAVE_GETPROCESSMEMORYINFO
#include <windows.h>
#include <psapi.h>
#endif

#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#if HAVE_TERMIOS_H
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <termios.h>
#elif HAVE_KBHIT
#include <conio.h>
#endif

#if HAVE_PTHREADS
#include <pthread.h>
#endif

#include <time.h>

#include "ffmpeg.h"
#include "cmdutils.h"

#include "libavutil/avassert.h"

const char program_name[] = "ffmpeg";
const int program_birth_year = 2000;

static FILE *vstats_file;

const char *const forced_keyframes_const_names[] = {
    "n",
    "n_forced",
    "prev_forced_n",
    "prev_forced_t",
    "t",
    NULL
};

static void do_video_stats(OutputStream *ost, int frame_size);
static int64_t getutime(void);
static int64_t getmaxrss(void);

static int run_as_daemon  = 0;
static int64_t video_size = 0;
static int64_t audio_size = 0;
static int64_t subtitle_size = 0;
static int64_t extra_size = 0;
static int nb_frames_dup = 0;
static int nb_frames_drop = 0;
static int64_t decode_error_stat[2];

static int current_time;
AVIOContext *progress_avio = NULL;

static uint8_t *subtitle_out;

#if HAVE_PTHREADS
/* signal to input threads that they should exit; set by the main thread */
static int transcoding_finished;
#endif

#define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"

InputStream **input_streams = NULL;
int        nb_input_streams = 0;
InputFile   **input_files   = NULL;
int        nb_input_files   = 0;

OutputStream **output_streams = NULL;
int         nb_output_streams = 0;
OutputFile   **output_files   = NULL;
int         nb_output_files   = 0;

FilterGraph **filtergraphs;
int        nb_filtergraphs;

#if HAVE_TERMIOS_H

/* init terminal so that we can grab keys */
static struct termios oldtty;
static int restore_tty;
#endif

const AVIOInterruptCB int_cb = { NULL, NULL };


void term_exit(void)
{
}

void term_init(void)
{
}

void assert_avoptions(AVDictionary *m)
{
}

int guess_input_channel_layout(InputStream *ist)
{
    return 1;
}

AVInputFormat *inputFormat = NULL;
AVFormatContext *pFC = NULL;
AVCodec *pCodec = NULL;
AVStream *pStream = NULL;
AVPacket packet = { 0 };
AVFrame* pSrc = NULL;
struct SwsContext *pSwsContext = NULL;
AVFrame* pDest = NULL;

int main(int argc, char **argv)
{
    av_register_all();

    inputFormat = av_find_input_format("image2");

    int res = avformat_open_input(&pFC, "F:\\test\\root_banner.jpg", inputFormat, NULL);

    res = avformat_find_stream_info(pFC, NULL);

    int idx = av_find_best_stream(pFC, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, -1);

    pStream = pFC->streams[idx];
    res = avcodec_open2(pStream->codec, pCodec, NULL);

    av_read_frame(pFC, &packet);

    int gotPicture;
    pSrc = avcodec_alloc_frame();
    avcodec_decode_video2(pStream->codec, pSrc, &gotPicture, &packet);

    av_free_packet(&packet);

    pSwsContext = sws_getCachedContext(pSwsContext,
        pSrc->width, pSrc->height,
        /*(PixelFormat)*/(pSrc->format),
        1, 60, PIX_FMT_BGRA,
        SWS_BILINEAR, NULL, NULL, NULL
    );

    pDest = avcodec_alloc_frame();
    avpicture_alloc((AVPicture *)(pDest), PIX_FMT_BGRA, 1, 60);
	avpicture_free((AVPicture *)(pDest));
	
	avpicture_alloc((AVPicture *)(pDest), PIX_FMT_BGRA, 1, 60);
    sws_scale(pSwsContext, pSrc->data, pSrc->linesize,
        0, pSrc->height, pDest->data, pDest->linesize
    );
	
    // in debug mode: HEAP[JpegTest.exe]: Heap block at 00706758 modified at 00706874 past requested size of 114
    avpicture_free((AVPicture *)(pDest));
    av_freep(&pDest);
    av_freep(&pSrc);
    avcodec_close(pStream->codec);
    avformat_close_input(&pFC);

    return 0;
}

