Skip to content

Commit 4f7c776

Browse files
author
Oliver
committed
play succeed
1 parent ed60c49 commit 4f7c776

File tree

11 files changed

+175
-94
lines changed

11 files changed

+175
-94
lines changed

bin/Player.apk

691 Bytes
Binary file not shown.

bin/classes.dex

1.62 KB
Binary file not shown.
137 Bytes
Binary file not shown.
1.21 KB
Binary file not shown.
488 Bytes
Binary file not shown.

jni/pack.c

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <pthread.h>
88

99
#include <android/log.h>
10-
#define INFO(msg) __android_log_write(ANDROID_LOG_INFO,"pack.c",msg);
10+
#define INFO(msg) __android_log_write(ANDROID_LOG_INFO,"native",msg);
1111

1212
#define RE(msg) return (*env)->NewStringUTF(env, msg);
1313

@@ -18,10 +18,17 @@ AVFormatContext *pFormatCtx;
1818
int i, videoStream, frameCount = 0;
1919
AVCodecContext *pCodecCtx;
2020
AVCodec *pCodec;
21-
AVFrame *pFrame;
21+
AVFrame *pFrame;
2222
AVPacket packet;
2323
int frameFinished;
2424
float aspect_ratio;
25+
26+
AVFrame *pFrameRGB;
27+
int numBytes;
28+
uint8_t *buffer;
29+
// BE for Big Endian, LE for Little Endian
30+
int dstFmt = PIX_FMT_RGB565;
31+
2532
struct SwsContext *img_convert_ctx;
2633
int width, height, bit_rate;
2734

@@ -166,6 +173,26 @@ jint Java_sysu_ss_xu_FFmpeg_getBitRate( JNIEnv* env, jobject thiz )
166173
return pCodecCtx->bit_rate;
167174
}
168175

176+
jboolean Java_sysu_ss_xu_FFmpeg_allocateBuffer( JNIEnv* env, jobject thiz )
177+
{
178+
// Allocate an AVFrame structure
179+
pFrameRGB=avcodec_alloc_frame();
180+
if(pFrameRGB==NULL)
181+
return 0;
182+
183+
// Determine required buffer size and allocate buffer
184+
numBytes=avpicture_get_size(dstFmt, pCodecCtx->width,
185+
pCodecCtx->height);
186+
buffer=(uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
187+
188+
// Assign appropriate parts of buffer to image planes in pFrameRGB
189+
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
190+
// of AVPicture
191+
avpicture_fill((AVPicture *)pFrameRGB, buffer, dstFmt, pCodecCtx->width, pCodecCtx->height);
192+
193+
return 1;
194+
}
195+
169196
/* for each decoded frame */
170197
jbyteArray Java_sysu_ss_xu_FFmpeg_getNextDecodedFrame( JNIEnv* env, jobject thiz )
171198
{
@@ -182,56 +209,52 @@ while(av_read_frame(pFormatCtx, &packet)>=0) {
182209
if(frameFinished) {
183210
INFO("got a frame");
184211

212+
/*
185213
AVPicture pict;
186214
187215
pict.linesize[0] = width;
188-
pict.linesize[1] = width / 2;
189-
pict.linesize[2] = width / 2;
190-
/*
191-
// tested ok.
192-
int pixelData1[ width * height ];
193-
int pixelData2[ width * height ];
194-
int pixelData3[ width * height ];
195-
pict.data[0] = pixelData1;
196-
pict.data[1] = pixelData2;
197-
pict.data[2] = pixelData3;
198-
*/
216+
pict.linesize[1] = width;
217+
pict.linesize[2] = width;
199218
200-
/* tested ok, too. Yay! */
201-
uint8_t pixelData[ width * height * 3 ];
219+
// tested ok, too. Yay!
220+
uint8_t pixelData[ width * height * 3 ];
221+
202222
pict.data[0] = &pixelData[ 0 ];
203223
pict.data[1] = &pixelData[ width * height ];
204224
pict.data[2] = &pixelData[ width * height * 2 ];
225+
//pict.data[3] = &pixelData[ width * height * 3 ];
226+
*/
205227

206-
INFO("memory prepared");
207-
208-
int dstFmt = PIX_FMT_YUV420P;
209228

210229
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, dstFmt, SWS_BICUBIC, NULL, NULL, NULL);
211230

212231
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize,
213-
0, pCodecCtx->height, pict.data, pict.linesize);
214-
INFO("frame format converted");
215-
sprintf(debugMsg, "*(pict.data[0]) %d", *(pict.data[0]));
232+
0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
233+
234+
/*
235+
sprintf(debugMsg, "*(pict.data[0]) %d", *(pFrameRGB->data[0]));
216236
INFO(debugMsg);
217-
sprintf(debugMsg, "*(pict.data[1]) %d", *(pict.data[1]));
237+
sprintf(debugMsg, "*(pict.data[1]) %d", *(pFrameRGB->data[1]));
218238
INFO(debugMsg);
219-
sprintf(debugMsg, "*(pict.data[2]) %d", *(pict.data[2]));
239+
sprintf(debugMsg, "*(pict.data[2]) %d", *(pFrameRGB->data[2]));
220240
INFO(debugMsg);
221-
sprintf(debugMsg, "sizeof %d %d", sizeof(pixelData[0]), sizeof(uint8_t));
241+
sprintf(debugMsg, "*(pict.data[3]) %d", *(pFrameRGB->data[3]));
222242
INFO(debugMsg);
223-
sprintf(debugMsg, "*pixelData %d", *pixelData);
243+
sprintf(debugMsg, "*(pict.linesize[0]) %d", pFrameRGB->linesize[0]);
224244
INFO(debugMsg);
245+
sprintf(debugMsg, "*(pict.linesize[1]) %d", pFrameRGB->linesize[1]);
246+
INFO(debugMsg);
247+
sprintf(debugMsg, "*(pict.linesize[2]) %d", pFrameRGB->linesize[2]);
248+
INFO(debugMsg);
249+
sprintf(debugMsg, "*(pict.linesize[3]) %d", pFrameRGB->linesize[3]);
250+
INFO(debugMsg);
251+
*/
225252

226253
++frameCount;
227-
INFO("++frameCount and return");
228254

229255
/* uint8_t == unsigned 8 bits == jboolean */
230-
jbyteArray nativePixels = (*env)->NewByteArray(env, width * height * 3);
231-
INFO("where failed 1");
232-
(*env)->SetByteArrayRegion(env, nativePixels, 0, width * height * 3, pixelData);
233-
INFO("where failed 2");
234-
256+
jbyteArray nativePixels = (*env)->NewByteArray(env, numBytes);
257+
(*env)->SetByteArrayRegion(env, nativePixels, 0, numBytes, buffer);
235258
return nativePixels;
236259
}
237260

libs/armeabi/libpack.so

-88 Bytes
Binary file not shown.

obj/local/armeabi/libpack.so

540 Bytes
Binary file not shown.

obj/local/armeabi/objs/pack/pack.o

456 Bytes
Binary file not shown.

src/sysu/ss/xu/FFmpeg.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class FFmpeg {
3232
public native int getWidth();
3333
public native int getHeight();
3434
public native int getBitRate();
35+
public native boolean allocateBuffer();
3536
public native byte[] getNextDecodedFrame();
3637

3738

@@ -80,11 +81,17 @@ private void init(String filePath) {
8081
else {
8182
Log.i("ff", "failed codec open");
8283
return;
83-
}
84-
84+
}
8585
Log.i("ff", getCodecName());
8686

87-
avcodecAllocFrame();
87+
avcodecAllocFrame();
88+
89+
if( allocateBuffer() )
90+
Log.i("ff", "success allocate buffer");
91+
else {
92+
Log.i("ff", "failed allocate buffer");
93+
return;
94+
}
8895
}
8996

9097
public void openFile(String filePath) {

0 commit comments

Comments
 (0)