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;
1818int i , videoStream , frameCount = 0 ;
1919AVCodecContext * pCodecCtx ;
2020AVCodec * pCodec ;
21- AVFrame * pFrame ;
21+ AVFrame * pFrame ;
2222AVPacket packet ;
2323int frameFinished ;
2424float 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+
2532struct SwsContext * img_convert_ctx ;
2633int 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 */
170197jbyteArray 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 ) {
183210INFO ("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
0 commit comments