Skip to content

Commit

Permalink
create working video decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
havlenapetr committed Aug 1, 2010
1 parent cd65948 commit 237e262
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 11 deletions.
7 changes: 3 additions & 4 deletions jni/libmediaplayer/decoder_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
DecoderAudio::DecoderAudio(AVCodecContext* codec_ctx,
struct DecoderAudioConfig* config)
{
mQueue = new PacketQueue();
mCodecCtx = codec_ctx;
mConfig = config;
}
Expand All @@ -27,9 +26,9 @@ bool DecoderAudio::prepare(const char *err)
mSamples = (int16_t *) av_malloc(mSamplesSize);

if(Output::AudioDriver_set(mConfig->streamType,
mConfig->sampleRate,
mConfig->format,
mConfig->channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
mConfig->sampleRate,
mConfig->format,
mConfig->channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
err = "Couldnt' set audio track";
return false;
}
Expand Down
87 changes: 82 additions & 5 deletions jni/libmediaplayer/decoder_video.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#include <android/log.h>
#include "decoder_video.h"

#include "output.h"

extern "C" {
#include "libswscale/swscale.h"
} // end of extern C

#define TAG "FFMpegVideoDecoder"

DecoderVideo::DecoderVideo(AVCodecContext* codec_ctx)
DecoderVideo::DecoderVideo(AVCodecContext* codec_ctx,
struct DecoderVideoConfig* config)
{
mQueue = new PacketQueue();
mCodecCtx = codec_ctx;
mConfig = config;
}

DecoderVideo::~DecoderVideo()
Expand All @@ -19,15 +26,85 @@ DecoderVideo::~DecoderVideo()

bool DecoderVideo::prepare(const char *err)
{
return false;
void* pixels;

mFrame = avcodec_alloc_frame();
if (mFrame == NULL) {
return false;
}

if(Output::VideoDriver_getPixels(mConfig->width,
mConfig->height,
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
return false;
}

// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill((AVPicture *) mFrame,
(uint8_t *)pixels,
PIX_FMT_RGB565,
mConfig->width,
mConfig->height);

return true;
}

bool DecoderVideo::process(AVPacket *packet)
{
return false;
int completed;

// Decode video frame
avcodec_decode_video(mCodecCtx,
mConfig->frame,
&completed,
packet->data,
packet->size);

if (completed) {
// Convert the image from its native format to RGB
sws_scale(mConfig->img_convert_ctx,
mConfig->frame->data,
mConfig->frame->linesize,
0,
mConfig->height,
mFrame->data,
mFrame->linesize);

Output::VideoDriver_updateSurface();
return true;
}
return false;
}

bool DecoderVideo::decode(void* ptr)
{
return false;
AVPacket pPacket;

__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video");

while(mDecoding)
{
if(mQueue->get(&pPacket, true) < 0)
{
mDecoding = false;
return false;
}
if(!process(&pPacket))
{
mDecoding = false;
return false;
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&pPacket);
}

__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video ended");

Output::VideoDriver_unregister();

// Free the RGB image
av_free(mFrame);
return true;
}
14 changes: 12 additions & 2 deletions jni/libmediaplayer/decoder_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@

#include "decoder.h"

struct DecoderVideoConfig
{
int width;
int height;
struct SwsContext* img_convert_ctx;
AVFrame* frame;
};

class DecoderVideo : public IDecoder
{
public:
DecoderVideo(AVCodecContext* codec_ctx);
DecoderVideo(AVCodecContext* codec_ctx,
struct DecoderVideoConfig* config);

~DecoderVideo();

private:

struct DecoderVideoConfig* mConfig;
AVFrame* mFrame;
bool prepare(const char *err);
bool decode(void* ptr);
bool process(AVPacket *packet);
Expand Down

0 comments on commit 237e262

Please sign in to comment.