Skip to content

Commit 379371f

Browse files
committed
update unused code
1 parent 0a1e72f commit 379371f

File tree

6 files changed

+99
-70
lines changed

6 files changed

+99
-70
lines changed

jni/libmediaplayer/decoder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ IDecoder::~IDecoder()
1616
stop();
1717
}
1818
free(mQueue);
19+
avcodec_close(mStream->codec);
1920
}
2021

2122
void IDecoder::enqueue(AVPacket* packet)

jni/libmediaplayer/decoder_audio.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ DecoderAudio::DecoderAudio(AVStream* stream) : IDecoder(stream)
1111

1212
DecoderAudio::~DecoderAudio()
1313
{
14-
if(mDecoding)
15-
{
16-
stop();
17-
}
1814
}
1915

2016
bool DecoderAudio::prepare(const char *err)

jni/libmediaplayer/decoder_video.cpp

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ extern "C" {
99

1010
#define TAG "FFMpegVideoDecoder"
1111

12+
static uint64_t global_video_pkt_pts = AV_NOPTS_VALUE;
13+
1214
DecoderVideo::DecoderVideo(AVStream* stream) : IDecoder(stream)
1315
{
16+
mStream->codec->get_buffer = getBuffer;
17+
mStream->codec->release_buffer = releaseBuffer;
1418
}
1519

1620
DecoderVideo::~DecoderVideo()
1721
{
18-
if(mDecoding)
19-
{
20-
stop();
21-
}
2222
}
2323

2424
bool DecoderVideo::prepare(const char *err)
@@ -71,9 +71,29 @@ bool DecoderVideo::prepare(const char *err)
7171
return true;
7272
}
7373

74+
double DecoderVideo::synchronize(AVFrame *src_frame, double pts) {
75+
76+
double frame_delay;
77+
78+
if (pts != 0) {
79+
/* if we have pts, set video clock to it */
80+
mVideoClock = pts;
81+
} else {
82+
/* if we aren't given a pts, set it to the clock */
83+
pts = mVideoClock;
84+
}
85+
/* update the video clock */
86+
frame_delay = av_q2d(mStream->codec->time_base);
87+
/* if we are repeating a frame, adjust clock accordingly */
88+
frame_delay += src_frame->repeat_pict * (frame_delay * 0.5);
89+
mVideoClock += frame_delay;
90+
return pts;
91+
}
92+
7493
bool DecoderVideo::process(AVPacket *packet)
7594
{
7695
int completed;
96+
int pts = 0;
7797

7898
// Decode video frame
7999
avcodec_decode_video(mStream->codec,
@@ -82,7 +102,18 @@ bool DecoderVideo::process(AVPacket *packet)
82102
packet->data,
83103
packet->size);
84104

105+
if (packet->dts == AV_NOPTS_VALUE && mTempFrame->opaque
106+
&& *(uint64_t*) mTempFrame->opaque != AV_NOPTS_VALUE) {
107+
pts = *(uint64_t *) mTempFrame->opaque;
108+
} else if (packet->dts != AV_NOPTS_VALUE) {
109+
pts = packet->dts;
110+
} else {
111+
pts = 0;
112+
}
113+
pts *= av_q2d(mStream->time_base);
114+
85115
if (completed) {
116+
pts = synchronize(mTempFrame, pts);
86117
// Convert the image from its native format to RGB
87118
sws_scale(mConvertCtx,
88119
mTempFrame->data,
@@ -131,3 +162,20 @@ bool DecoderVideo::decode(void* ptr)
131162

132163
return true;
133164
}
165+
166+
/* These are called whenever we allocate a frame
167+
* buffer. We use this to store the global_pts in
168+
* a frame at the time it is allocated.
169+
*/
170+
int DecoderVideo::getBuffer(struct AVCodecContext *c, AVFrame *pic) {
171+
int ret = avcodec_default_get_buffer(c, pic);
172+
uint64_t *pts = (uint64_t *)av_malloc(sizeof(uint64_t));
173+
*pts = global_video_pkt_pts;
174+
pic->opaque = pts;
175+
return ret;
176+
}
177+
void DecoderVideo::releaseBuffer(struct AVCodecContext *c, AVFrame *pic) {
178+
if (pic)
179+
av_freep(&pic->opaque);
180+
avcodec_default_release_buffer(c, pic);
181+
}

jni/libmediaplayer/decoder_video.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ class DecoderVideo : public IDecoder
1313
AVFrame* mFrame;
1414
AVFrame* mTempFrame;
1515
struct SwsContext* mConvertCtx;
16+
double mVideoClock;
1617

1718
bool prepare(const char *err);
19+
double synchronize(AVFrame *src_frame, double pts);
1820
bool decode(void* ptr);
1921
bool process(AVPacket *packet);
22+
static int getBuffer(struct AVCodecContext *c, AVFrame *pic);
23+
static void releaseBuffer(struct AVCodecContext *c, AVFrame *pic);
2024
};
2125

2226
#endif //FFMPEG_DECODER_AUDIO_H

jni/libmediaplayer/mediaplayer.cpp

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,67 +57,63 @@ MediaPlayer::~MediaPlayer()
5757
status_t MediaPlayer::prepareAudio()
5858
{
5959
__android_log_print(ANDROID_LOG_INFO, TAG, "prepareAudio");
60-
mFFmpegStorage.audio.stream = -1;
61-
for (int i = 0; i < mFFmpegStorage.pFormatCtx->nb_streams; i++) {
62-
if (mFFmpegStorage.pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
63-
mFFmpegStorage.audio.stream = i;
64-
}
65-
if(mFFmpegStorage.audio.stream != -1) {
60+
mAudioStreamIndex = -1;
61+
for (int i = 0; i < mMovieFile->nb_streams; i++) {
62+
if (mMovieFile->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
63+
mAudioStreamIndex = i;
6664
break;
6765
}
6866
}
6967

70-
if (mFFmpegStorage.audio.stream == -1) {
68+
if (mAudioStreamIndex == -1) {
7169
return INVALID_OPERATION;
7270
}
7371

7472
// Get a pointer to the codec context for the video stream
75-
mFFmpegStorage.audio.codec_ctx = mFFmpegStorage.pFormatCtx->streams[mFFmpegStorage.audio.stream]->codec;
76-
mFFmpegStorage.audio.codec = avcodec_find_decoder(mFFmpegStorage.audio.codec_ctx->codec_id);
77-
if (mFFmpegStorage.audio.codec == NULL) {
73+
AVCodecContext* codec_ctx = mMovieFile->streams[mAudioStreamIndex]->codec;
74+
AVCodec* codec = avcodec_find_decoder(codec_ctx->codec_id);
75+
if (codec == NULL) {
7876
return INVALID_OPERATION;
7977
}
8078

8179
// Open codec
82-
if (avcodec_open(mFFmpegStorage.audio.codec_ctx,
83-
mFFmpegStorage.audio.codec) < 0) {
84-
return INVALID_OPERATION; }
80+
if (avcodec_open(codec_ctx, codec) < 0) {
81+
return INVALID_OPERATION;
82+
}
8583
return NO_ERROR;
8684
}
8785

8886
status_t MediaPlayer::prepareVideo()
8987
{
9088
__android_log_print(ANDROID_LOG_INFO, TAG, "prepareVideo");
9189
// Find the first video stream
92-
mFFmpegStorage.video.stream = -1;
93-
for (int i = 0; i < mFFmpegStorage.pFormatCtx->nb_streams; i++) {
94-
if (mFFmpegStorage.pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
95-
mFFmpegStorage.video.stream = i;
96-
}
97-
if(mFFmpegStorage.video.stream != -1) {
90+
mVideoStreamIndex = -1;
91+
for (int i = 0; i < mMovieFile->nb_streams; i++) {
92+
if (mMovieFile->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
93+
mVideoStreamIndex = i;
9894
break;
9995
}
10096
}
10197

102-
if (mFFmpegStorage.video.stream == -1) {
98+
if (mVideoStreamIndex == -1) {
10399
return INVALID_OPERATION;
104100
}
105101

106102
// Get a pointer to the codec context for the video stream
107-
mFFmpegStorage.video.codec_ctx = mFFmpegStorage.pFormatCtx->streams[mFFmpegStorage.video.stream]->codec;
108-
mFFmpegStorage.video.codec = avcodec_find_decoder(mFFmpegStorage.video.codec_ctx->codec_id);
109-
if (mFFmpegStorage.video.codec == NULL) {
103+
AVCodecContext* codec_ctx = mMovieFile->streams[mVideoStreamIndex]->codec;
104+
AVCodec* codec = avcodec_find_decoder(codec_ctx->codec_id);
105+
if (codec == NULL) {
110106
return INVALID_OPERATION;
111107
}
112108

113109
// Open codec
114-
if (avcodec_open(mFFmpegStorage.video.codec_ctx, mFFmpegStorage.video.codec) < 0) {
110+
if (avcodec_open(codec_ctx, codec) < 0) {
115111
return INVALID_OPERATION;
116112
}
117113

118-
mVideoWidth = mFFmpegStorage.video.codec_ctx->width;
119-
mVideoHeight = mFFmpegStorage.video.codec_ctx->height;
120-
mDuration = mFFmpegStorage.pFormatCtx->duration;
114+
mVideoWidth = codec_ctx->width;
115+
mVideoHeight = codec_ctx->height;
116+
mDuration = mMovieFile->duration;
121117

122118
return NO_ERROR;
123119
}
@@ -151,11 +147,11 @@ status_t MediaPlayer::setDataSource(const char *url)
151147
__android_log_print(ANDROID_LOG_INFO, TAG, "setDataSource(%s)", url);
152148
status_t err = BAD_VALUE;
153149
// Open video file
154-
if(av_open_input_file(&mFFmpegStorage.pFormatCtx, url, NULL, 0, NULL) != 0) {
150+
if(av_open_input_file(&mMovieFile, url, NULL, 0, NULL) != 0) {
155151
return INVALID_OPERATION;
156152
}
157153
// Retrieve stream information
158-
if(av_find_stream_info(mFFmpegStorage.pFormatCtx) < 0) {
154+
if(av_find_stream_info(mMovieFile) < 0) {
159155
return INVALID_OPERATION;
160156
}
161157
mCurrentState = MEDIA_PLAYER_INITIALIZED;
@@ -180,11 +176,13 @@ status_t MediaPlayer::suspend() {
180176
__android_log_print(ANDROID_LOG_ERROR, TAG, "suspended");
181177

182178
// Close the codec
183-
avcodec_close(mFFmpegStorage.video.codec_ctx);
184-
avcodec_close(mFFmpegStorage.audio.codec_ctx);
179+
free(mDecoderAudio);
180+
free(mDecoderVideo);
181+
//avcodec_close(mFFmpegStorage.video.codec_ctx);
182+
//avcodec_close(mFFmpegStorage.audio.codec_ctx);
185183

186184
// Close the video file
187-
av_close_input_file(mFFmpegStorage.pFormatCtx);
185+
av_close_input_file(mMovieFile);
188186
return NO_ERROR;
189187
}
190188

@@ -236,15 +234,15 @@ void MediaPlayer::decodeMovie(void* ptr)
236234
AVPacket pPacket;
237235
char err[256];
238236

239-
AVStream* stream_audio = mFFmpegStorage.pFormatCtx->streams[mFFmpegStorage.audio.stream];
237+
AVStream* stream_audio = mMovieFile->streams[mAudioStreamIndex];
240238
mDecoderAudio = new DecoderAudio(stream_audio);
241239
if(!mDecoderAudio->startAsync(err))
242240
{
243241
__android_log_print(ANDROID_LOG_INFO, TAG, "Couldn't start audio decoder: %s", err);
244242
return;
245243
}
246244

247-
AVStream* stream_video = mFFmpegStorage.pFormatCtx->streams[mFFmpegStorage.video.stream];
245+
AVStream* stream_video = mMovieFile->streams[mVideoStreamIndex];
248246
mDecoderVideo = new DecoderVideo(stream_video);
249247
if(!mDecoderVideo->startAsync(err))
250248
{
@@ -257,22 +255,22 @@ void MediaPlayer::decodeMovie(void* ptr)
257255
while (mCurrentState != MEDIA_PLAYER_DECODED && mCurrentState != MEDIA_PLAYER_STOPPED &&
258256
mCurrentState != MEDIA_PLAYER_STATE_ERROR)
259257
{
260-
if (mDecoderVideo->packets()/*mVideoQueue->size()*/ > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
258+
if (mDecoderVideo->packets() > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
261259
mDecoderAudio->packets() > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
262260
usleep(200);
263261
continue;
264262
}
265263

266-
if(av_read_frame(mFFmpegStorage.pFormatCtx, &pPacket) < 0) {
264+
if(av_read_frame(mMovieFile, &pPacket) < 0) {
267265
mCurrentState = MEDIA_PLAYER_DECODED;
268266
continue;
269267
}
270268

271269
// Is this a packet from the video stream?
272-
if (pPacket.stream_index == mFFmpegStorage.video.stream) {
270+
if (pPacket.stream_index == mVideoStreamIndex) {
273271
mDecoderVideo->enqueue(&pPacket);
274272
}
275-
else if (pPacket.stream_index == mFFmpegStorage.audio.stream) {
273+
else if (pPacket.stream_index == mAudioStreamIndex) {
276274
mDecoderAudio->enqueue(&pPacket);
277275
}
278276
else {
@@ -360,7 +358,8 @@ status_t MediaPlayer::getCurrentPosition(int *msec)
360358
if (mCurrentState < MEDIA_PLAYER_PREPARED) {
361359
return INVALID_OPERATION;
362360
}
363-
*msec = mTime * 1000;
361+
*msec = 0/*av_gettime()*/;
362+
//__android_log_print(ANDROID_LOG_INFO, TAG, "position %i", *msec);
364363
return NO_ERROR;
365364
}
366365

jni/libmediaplayer/mediaplayer.h

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,30 +141,8 @@ class MediaPlayer
141141
// status_t getMetadata(bool update_only, bool apply_filter, Parcel *metadata);
142142
status_t suspend();
143143
status_t resume();
144+
144145
private:
145-
146-
struct ffmpeg_video_t {
147-
bool initzialized;
148-
int stream;
149-
AVCodecContext *codec_ctx;
150-
AVCodec *codec;
151-
};
152-
153-
struct ffmpeg_audio_t {
154-
bool initzialized;
155-
bool decode;
156-
int stream;
157-
AVCodecContext *codec_ctx;
158-
AVCodec *codec;
159-
};
160-
161-
// ffmpeg store struct
162-
struct ffmpeg_fields_t {
163-
AVFormatContext *pFormatCtx;
164-
struct ffmpeg_video_t video;
165-
struct ffmpeg_audio_t audio;
166-
} mFFmpegStorage;
167-
168146
status_t prepareAudio();
169147
status_t prepareVideo();
170148
bool shouldCancel(PacketQueue* queue);
@@ -180,6 +158,9 @@ class MediaPlayer
180158
//Mutex mNotifyLock;
181159
//Condition mSignal;
182160
MediaPlayerListener* mListener;
161+
AVFormatContext* mMovieFile;
162+
int mAudioStreamIndex;
163+
int mVideoStreamIndex;
183164
DecoderAudio* mDecoderAudio;
184165
DecoderVideo* mDecoderVideo;
185166
void* mCookie;

0 commit comments

Comments
 (0)