/* Use the FFMPEG library API to encode camera
 * frames to a MPEG file or decode an existing
 * video file.  Images are returned in IplImage
 * format and RGB colormap for compatibility
 * with OpenCV functions.
 */

#ifndef __VIDEO_H
#define __VIDEO_H

#include <avcodec.h>
#include <avformat.h>
#include <opencv/cv.h>
#include <sys/types.h>	// For uint8_t type
#include <stdio.h>		// For FILE type

struct video {
	AVFormatContext *pFormatCtx;
	int i, videoStream;

	AVCodec *pCodec;
	AVCodecContext *pCodecCtx;
	AVFrame *pFrame;
	AVFrame *pFrameRGB;

	int numBytes;
	uint8_t *buffer;
	uint8_t *frame_buffer;

	int task;
	FILE *file;
};

typedef struct video video_t;

// Specify encoding or decoding during
// video initialization.
enum {
	VIDEO_ENCODE,
	VIDEO_DECODE
};

// Initialize the video struct using the
// given file name and specified encoding
// or decoding task. 
video_t * video_init(char *file, int task);

// Functions for encoding video.
int video_prepare_encode(video_t *, int, int);
void video_encode_frame(video_t *, char *);
void video_data_to_frame(video_t *, char*);
void video_add_delayed(video_t *);

// Functions for decoding video.
int video_decode_frame(video_t *);
int video_get_next_frame(video_t *);
void video_get_image(video_t *, IplImage *);

// Finalize the video and free all memory.
void video_encode_close(video_t *);
void video_decode_close(video_t *);

#endif

