Skip to content

Commit 237e262

Browse files
committed
create working video decoder
1 parent cd65948 commit 237e262

File tree

3 files changed

+97
-11
lines changed

3 files changed

+97
-11
lines changed

jni/libmediaplayer/decoder_audio.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
DecoderAudio::DecoderAudio(AVCodecContext* codec_ctx,
99
struct DecoderAudioConfig* config)
1010
{
11-
mQueue = new PacketQueue();
1211
mCodecCtx = codec_ctx;
1312
mConfig = config;
1413
}
@@ -27,9 +26,9 @@ bool DecoderAudio::prepare(const char *err)
2726
mSamples = (int16_t *) av_malloc(mSamplesSize);
2827

2928
if(Output::AudioDriver_set(mConfig->streamType,
30-
mConfig->sampleRate,
31-
mConfig->format,
32-
mConfig->channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
29+
mConfig->sampleRate,
30+
mConfig->format,
31+
mConfig->channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
3332
err = "Couldnt' set audio track";
3433
return false;
3534
}

jni/libmediaplayer/decoder_video.cpp

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#include <android/log.h>
22
#include "decoder_video.h"
33

4+
#include "output.h"
5+
6+
extern "C" {
7+
#include "libswscale/swscale.h"
8+
} // end of extern C
9+
410
#define TAG "FFMpegVideoDecoder"
511

6-
DecoderVideo::DecoderVideo(AVCodecContext* codec_ctx)
12+
DecoderVideo::DecoderVideo(AVCodecContext* codec_ctx,
13+
struct DecoderVideoConfig* config)
714
{
8-
mQueue = new PacketQueue();
915
mCodecCtx = codec_ctx;
16+
mConfig = config;
1017
}
1118

1219
DecoderVideo::~DecoderVideo()
@@ -19,15 +26,85 @@ DecoderVideo::~DecoderVideo()
1926

2027
bool DecoderVideo::prepare(const char *err)
2128
{
22-
return false;
29+
void* pixels;
30+
31+
mFrame = avcodec_alloc_frame();
32+
if (mFrame == NULL) {
33+
return false;
34+
}
35+
36+
if(Output::VideoDriver_getPixels(mConfig->width,
37+
mConfig->height,
38+
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
39+
return false;
40+
}
41+
42+
// Assign appropriate parts of buffer to image planes in pFrameRGB
43+
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
44+
// of AVPicture
45+
avpicture_fill((AVPicture *) mFrame,
46+
(uint8_t *)pixels,
47+
PIX_FMT_RGB565,
48+
mConfig->width,
49+
mConfig->height);
50+
51+
return true;
2352
}
2453

2554
bool DecoderVideo::process(AVPacket *packet)
2655
{
27-
return false;
56+
int completed;
57+
58+
// Decode video frame
59+
avcodec_decode_video(mCodecCtx,
60+
mConfig->frame,
61+
&completed,
62+
packet->data,
63+
packet->size);
64+
65+
if (completed) {
66+
// Convert the image from its native format to RGB
67+
sws_scale(mConfig->img_convert_ctx,
68+
mConfig->frame->data,
69+
mConfig->frame->linesize,
70+
0,
71+
mConfig->height,
72+
mFrame->data,
73+
mFrame->linesize);
74+
75+
Output::VideoDriver_updateSurface();
76+
return true;
77+
}
78+
return false;
2879
}
2980

3081
bool DecoderVideo::decode(void* ptr)
3182
{
32-
return false;
83+
AVPacket pPacket;
84+
85+
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video");
86+
87+
while(mDecoding)
88+
{
89+
if(mQueue->get(&pPacket, true) < 0)
90+
{
91+
mDecoding = false;
92+
return false;
93+
}
94+
if(!process(&pPacket))
95+
{
96+
mDecoding = false;
97+
return false;
98+
}
99+
// Free the packet that was allocated by av_read_frame
100+
av_free_packet(&pPacket);
101+
}
102+
103+
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding video ended");
104+
105+
Output::VideoDriver_unregister();
106+
107+
// Free the RGB image
108+
av_free(mFrame);
109+
return true;
33110
}

jni/libmediaplayer/decoder_video.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@
33

44
#include "decoder.h"
55

6+
struct DecoderVideoConfig
7+
{
8+
int width;
9+
int height;
10+
struct SwsContext* img_convert_ctx;
11+
AVFrame* frame;
12+
};
13+
614
class DecoderVideo : public IDecoder
715
{
816
public:
9-
DecoderVideo(AVCodecContext* codec_ctx);
17+
DecoderVideo(AVCodecContext* codec_ctx,
18+
struct DecoderVideoConfig* config);
1019

1120
~DecoderVideo();
1221

1322
private:
14-
23+
struct DecoderVideoConfig* mConfig;
24+
AVFrame* mFrame;
1525
bool prepare(const char *err);
1626
bool decode(void* ptr);
1727
bool process(AVPacket *packet);

0 commit comments

Comments
 (0)