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.

bin/classes/sysu/ss/xu/FFmpeg.class

137 Bytes
Binary file not shown.
Binary file not shown.
488 Bytes
Binary file not shown.

jni/pack.c

+54-31
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

+10-3
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) {

src/sysu/ss/xu/PlayerActivity.java

+111-60
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package sysu.ss.xu;
22

3+
import java.nio.ByteBuffer;
4+
import java.nio.ShortBuffer;
5+
36
import android.app.Activity;
47
import android.content.Context;
58
import android.graphics.Bitmap;
@@ -24,110 +27,158 @@ public class PlayerActivity extends Activity {
2427
public void onCreate(Bundle savedInstanceState) {
2528
super.onCreate(savedInstanceState);
2629

27-
30+
// test();
31+
32+
DisplayMetrics dm = new DisplayMetrics();
33+
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
34+
setContentView(new PlayerView(this, dm.widthPixels, dm.heightPixels));
2835

29-
// TextView tv = new TextView(this);
30-
// setContentView(tv);
31-
//
32-
FFmpeg ff = new FFmpeg();
36+
37+
}
38+
39+
40+
private void test() {
41+
FFmpeg ff = new FFmpeg();
3342

3443

35-
ff.openFile("/mnt/sdcard/ipod.m4v");
44+
ff.openFile("/mnt/sdcard/mpeg2.m2v");
3645
int width = ff.getWidth();
3746
int height = ff.getHeight();
3847
int bitrate = ff.getBitRate();
3948

4049
Log.i("main", "w " + width);
4150
Log.i("main", "h " + height);
4251
Log.i("main", "br " + bitrate);
52+
int linesize = width * height;
4353
byte[] pixels = ff.getNextDecodedFrame();
44-
Log.i("main", "" + pixels.length);
45-
Log.i("main", "0 " + pixels[0]);
46-
Log.i("main", "1 " + pixels[width * height]);
47-
Log.i("main", "2 " + pixels[width * height * 2]);
48-
49-
50-
51-
52-
53-
54-
//setContentView(R.layout.main);
55-
// DisplayMetrics dm = new DisplayMetrics();
56-
// this.getWindowManager().getDefaultDisplay().getMetrics(dm);
57-
// setContentView(new PlayerView(this, dm.widthPixels, dm.heightPixels));
58-
// DisplayMetrics dm = new DisplayMetrics();
59-
// this.getWindowManager().getDefaultDisplay().getMetrics(dm);
60-
// gv = new GameView(this,dm.widthPixels,dm.heightPixels);
61-
// setContentView(gv);
62-
}
63-
64-
65-
class PlayerView extends View implements Runnable {
54+
}
55+
56+
57+
class PlayerView extends View implements Runnable {
6658

6759
private Bitmap bitmap;
6860
private Paint p;
6961
private FFmpeg ff;
7062
private int width;
7163
private int height;
72-
private int bitrate;
73-
private int[] framePixels;
7464
private int bitmapWidth;
65+
private byte[] nativePixels;
66+
private int[] pixels;
67+
private ByteBuffer buffer;
7568

7669

7770
public PlayerView(Context context, int DisplayWidth, int DisplayHeight) {
7871
super(context);
7972

80-
bitmap = Bitmap.createBitmap(DisplayWidth, DisplayHeight, Bitmap.Config.RGB_565);
8173
//b = BitmapFactory.decodeResource(this.getResources(), R.drawable.qq);
8274
p = new Paint();
8375

84-
Log.i("pp", "w: " + DisplayWidth + " h: " + DisplayHeight);
85-
86-
bitmapWidth = bitmap.getWidth();
87-
bitmap.setHasAlpha(true);
88-
89-
byte r = 0; r <<= 3;
90-
byte g = (byte) 255; g <<= 2;
91-
byte b = 0; b <<= 3;
92-
int pixelValue=Color.argb(255, r, g, b);
93-
for(int j = 0; j < 30; j++)
94-
{
95-
for(int i = 0; i < 30; i++) {
96-
bitmap.setPixel(30 + i, 30 + j, pixelValue);
97-
}
98-
}
76+
// testSetPixel();
9977

10078
ff = new FFmpeg();
101-
ff.openFile("/mnt/sdcard/ipod.m4v");
79+
ff.openFile("/mnt/sdcard/mpeg2.m2v");
80+
//ff.openFile("/mnt/sdcard/a.mp4");
10281
width = ff.getWidth();
10382
height = ff.getHeight();
104-
bitrate = ff.getBitRate();
10583

106-
Log.i("bm", "w " + width);
107-
Log.i("bm", "h " + height);
108-
Log.i("bm", "br " + bitrate);
84+
// bitmap = Bitmap.createBitmap(DisplayWidth, DisplayHeight, Bitmap.Config.RGB_565);
85+
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
86+
87+
Log.i("view", "ff w " + width);
88+
Log.i("view", "ff h " + height);
89+
90+
nativePixels = ff.getNextDecodedFrame();
91+
Log.i("view", "native.length " + nativePixels.length);
92+
Log.i("view", "needed bytes of pixels " + (width * height * 2));
93+
94+
// buffer = ShortBuffer.allocate(width * height);
95+
buffer = ByteBuffer.wrap(nativePixels);
96+
97+
// renderCopy();
98+
// testRenderCopy();
99+
100+
bitmap.copyPixelsFromBuffer(buffer);
109101

110102
/* 开启线程 */
111103
new Thread(this).start();
112104

113105
Log.i("player view", "constructor");
114-
}
115-
public void onDraw(Canvas canvas){
106+
}
107+
108+
private void testRenderCopy() {
109+
// TODO Auto-generated method stub
110+
int rIndex, gIndex, bIndex;
111+
short temp;
112+
rIndex = 0;
113+
gIndex = width * height;
114+
bIndex = width * height * 2;
115+
temp = 0x0000;
116+
117+
Log.i("test", "native R " + Integer.toHexString(nativePixels[rIndex]));
118+
Log.i("test", "native G " + Integer.toHexString(nativePixels[gIndex]));
119+
Log.i("test", "native B " + Integer.toHexString(nativePixels[bIndex]));
120+
121+
temp = (short) (nativePixels[rIndex] << 11);
122+
Log.i("test", Integer.toHexString(temp));
123+
temp = (short) (temp | ((short) (nativePixels[gIndex] << 5)));
124+
Log.i("test", Integer.toHexString(temp));
125+
temp = (short) (temp | ((short) (nativePixels[bIndex])));
126+
Log.i("test", Integer.toHexString(temp));
127+
}
128+
129+
private void testSetPixel() {
130+
bitmap.setHasAlpha(true);
131+
132+
byte r = 0; r <<= 3;
133+
byte g = (byte) 255; g <<= 2;
134+
byte b = 0; b <<= 3;
135+
int pixelValue=Color.argb(255, r, g, b);
136+
for(int j = 0; j < 30; j++)
137+
{
138+
for(int i = 0; i < 30; i++) {
139+
bitmap.setPixel(30 + i, 30 + j, pixelValue);
140+
}
141+
}
142+
}
143+
144+
public void onDraw(Canvas canvas){
116145
super.onDraw(canvas);
117146

118-
canvas.drawColor(Color.RED);
147+
canvas.drawColor(Color.GREEN);
119148

120-
//framePixels = ff.getNextDecodedFrame();
121-
ff.getNextDecodedFrame();
122-
//bitmap.setPixels(framePixels, 0, bitmapWidth, 0, 0, width, height);
123-
124-
//canvas.drawBitmap(bitmap, 0, 0, p);
149+
nativePixels = ff.getNextDecodedFrame();
150+
buffer = ByteBuffer.wrap(nativePixels);
151+
bitmap.copyPixelsFromBuffer(buffer);
152+
153+
canvas.drawBitmap(bitmap, 0, 0, p);
125154

126155

127156
}
128157

129158

130-
public void run() {
159+
private void renderCopy() {
160+
//TODO
161+
int rIndex, gIndex, bIndex;
162+
short temp;
163+
rIndex = 0;
164+
gIndex = width * height;
165+
bIndex = width * height * 2;
166+
temp = 0x0000;
167+
168+
buffer.rewind();
169+
for(int loop = 0; loop < buffer.capacity(); loop++) {
170+
temp = (short) (nativePixels[rIndex] << 11);
171+
temp = (short) (temp | ((short) (nativePixels[gIndex] << 5)));
172+
temp = (short) (temp | ((short) (nativePixels[bIndex])));
173+
// buffer.put( temp );
174+
175+
++rIndex;
176+
++gIndex;
177+
++bIndex;
178+
}
179+
buffer.rewind();
180+
}
181+
public void run() {
131182
while (!Thread.currentThread().isInterrupted()) {
132183
try {
133184
Thread.sleep(100);

0 commit comments

Comments
 (0)