Skip to content

Commit 9a96199

Browse files
committed
working video decoder callback
1 parent 116072a commit 9a96199

File tree

4 files changed

+51
-81
lines changed

4 files changed

+51
-81
lines changed

jni/libmediaplayer/decoder_video.cpp

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
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-
104
#define TAG "FFMpegVideoDecoder"
115

126
static uint64_t global_video_pkt_pts = AV_NOPTS_VALUE;
@@ -23,51 +17,10 @@ DecoderVideo::~DecoderVideo()
2317

2418
bool DecoderVideo::prepare()
2519
{
26-
void* pixels;
27-
2820
mFrame = avcodec_alloc_frame();
2921
if (mFrame == NULL) {
30-
//err = "Couldn't allocate mFrame";
31-
return false;
32-
}
33-
34-
mTempFrame = avcodec_alloc_frame();
35-
if (mTempFrame == NULL) {
36-
//err = "Couldn't allocate mTempFrame";
37-
return false;
38-
}
39-
40-
mConvertCtx = sws_getContext(mStream->codec->width,
41-
mStream->codec->height,
42-
mStream->codec->pix_fmt,
43-
mStream->codec->width,
44-
mStream->codec->height,
45-
PIX_FMT_RGB565,
46-
SWS_POINT,
47-
NULL,
48-
NULL,
49-
NULL);
50-
if (mConvertCtx == NULL) {
51-
//err = "Couldn't allocate mConvertCtx";
52-
return false;
53-
}
54-
55-
if(Output::VideoDriver_getPixels(mStream->codec->width,
56-
mStream->codec->height,
57-
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
58-
//err = "Couldn't get pixels from android surface wrapper";
5922
return false;
6023
}
61-
62-
// Assign appropriate parts of buffer to image planes in pFrameRGB
63-
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
64-
// of AVPicture
65-
avpicture_fill((AVPicture *) mFrame,
66-
(uint8_t *)pixels,
67-
PIX_FMT_RGB565,
68-
mStream->codec->width,
69-
mStream->codec->height);
70-
7124
return true;
7225
}
7326

@@ -97,14 +50,14 @@ bool DecoderVideo::process(AVPacket *packet)
9750

9851
// Decode video frame
9952
avcodec_decode_video(mStream->codec,
100-
mTempFrame,
53+
mFrame,
10154
&completed,
10255
packet->data,
10356
packet->size);
10457

105-
if (packet->dts == AV_NOPTS_VALUE && mTempFrame->opaque
106-
&& *(uint64_t*) mTempFrame->opaque != AV_NOPTS_VALUE) {
107-
pts = *(uint64_t *) mTempFrame->opaque;
58+
if (packet->dts == AV_NOPTS_VALUE && mFrame->opaque
59+
&& *(uint64_t*) mFrame->opaque != AV_NOPTS_VALUE) {
60+
pts = *(uint64_t *) mFrame->opaque;
10861
} else if (packet->dts != AV_NOPTS_VALUE) {
10962
pts = packet->dts;
11063
} else {
@@ -113,20 +66,10 @@ bool DecoderVideo::process(AVPacket *packet)
11366
pts *= av_q2d(mStream->time_base);
11467

11568
if (completed) {
116-
pts = synchronize(mTempFrame, pts);
69+
pts = synchronize(mFrame, pts);
11770

118-
//onDecode(frame, pts);
71+
onDecode(mFrame, pts);
11972

120-
// Convert the image from its native format to RGB
121-
sws_scale(mConvertCtx,
122-
mTempFrame->data,
123-
mTempFrame->linesize,
124-
0,
125-
mStream->codec->height,
126-
mFrame->data,
127-
mFrame->linesize);
128-
129-
Output::VideoDriver_updateSurface();
13073
return true;
13174
}
13275
return false;
@@ -158,8 +101,6 @@ bool DecoderVideo::decode(void* ptr)
158101

159102
// Free the RGB image
160103
av_free(mFrame);
161-
// Free the RGB image
162-
av_free(mTempFrame);
163104

164105
return true;
165106
}

jni/libmediaplayer/decoder_video.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ class DecoderVideo : public IDecoder
1515

1616
private:
1717
AVFrame* mFrame;
18-
AVFrame* mTempFrame;
19-
struct SwsContext* mConvertCtx;
2018
double mVideoClock;
2119

2220
bool prepare();

jni/libmediaplayer/mediaplayer.cpp

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ status_t MediaPlayer::prepareVideo()
112112
return INVALID_OPERATION;
113113
}
114114

115+
AVStream* stream = mMovieFile->streams[mVideoStreamIndex];
115116
// Get a pointer to the codec context for the video stream
116-
AVCodecContext* codec_ctx = mMovieFile->streams[mVideoStreamIndex]->codec;
117+
AVCodecContext* codec_ctx = stream->codec;
117118
AVCodec* codec = avcodec_find_decoder(codec_ctx->codec_id);
118119
if (codec == NULL) {
119120
return INVALID_OPERATION;
@@ -128,6 +129,41 @@ status_t MediaPlayer::prepareVideo()
128129
mVideoHeight = codec_ctx->height;
129130
mDuration = mMovieFile->duration;
130131

132+
mConvertCtx = sws_getContext(stream->codec->width,
133+
stream->codec->height,
134+
stream->codec->pix_fmt,
135+
stream->codec->width,
136+
stream->codec->height,
137+
PIX_FMT_RGB565,
138+
SWS_POINT,
139+
NULL,
140+
NULL,
141+
NULL);
142+
143+
if (mConvertCtx == NULL) {
144+
return INVALID_OPERATION;
145+
}
146+
147+
void* pixels;
148+
if (Output::VideoDriver_getPixels(stream->codec->width,
149+
stream->codec->height,
150+
&pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
151+
return INVALID_OPERATION;
152+
}
153+
154+
mFrame = avcodec_alloc_frame();
155+
if (mFrame == NULL) {
156+
return INVALID_OPERATION;
157+
}
158+
// Assign appropriate parts of buffer to image planes in pFrameRGB
159+
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
160+
// of AVPicture
161+
avpicture_fill((AVPicture *) mFrame,
162+
(uint8_t *) pixels,
163+
PIX_FMT_RGB565,
164+
stream->codec->width,
165+
stream->codec->height);
166+
131167
return NO_ERROR;
132168
}
133169

@@ -248,23 +284,16 @@ bool MediaPlayer::shouldCancel(PacketQueue* queue)
248284

249285
void MediaPlayer::decode(AVFrame* frame, double pts)
250286
{
251-
/*
252-
AVFrame* buffFrame = avcodec_alloc_frame();
253-
if (frame == NULL) {
254-
return false;
255-
}
256-
257287
// Convert the image from its native format to RGB
258-
sws_scale(mConvertCtx,
259-
frame->data,
260-
frame->linesize,
288+
sws_scale(sPlayer->mConvertCtx,
289+
frame->data,
290+
frame->linesize,
261291
0,
262-
mStream->codec->height,
263-
buffFrame->data,
264-
buffFrame->linesize);
292+
sPlayer->mVideoHeight,
293+
sPlayer->mFrame->data,
294+
sPlayer->mFrame->linesize);
265295

266296
Output::VideoDriver_updateSurface();
267-
*/
268297
}
269298

270299
void MediaPlayer::decode(int16_t* buffer, int buffer_size)

jni/libmediaplayer/mediaplayer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class MediaPlayer
165165
int mVideoStreamIndex;
166166
DecoderAudio* mDecoderAudio;
167167
DecoderVideo* mDecoderVideo;
168+
AVFrame* mFrame;
169+
struct SwsContext* mConvertCtx;
168170

169171
void* mCookie;
170172
media_player_states mCurrentState;

0 commit comments

Comments
 (0)