• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with avcodec_find_decoder()

cervell0

Lurker
Feb 5, 2011
1
0
I'm trying to decode a video but in my code I think there is a problem about avcodec_find decoder().. I control it with log_message and on logcat there is "here1" but there isn't "here2" .. So I think there is a problem with avcodec_find_decoder...
Can anyone any idea?? Please help me, Thanks

And how can I describe pCodecCtx->codec_id.. With %d and %s It didn't work.

char info[40];
sprintf(info,"i degeri = %d",pCodecCtx->codec_id);
log_message(info);


*****Here is my code******

jint Java_com_test_Test_takePics(JNIEnv* env, jobject javaThis) {
int framecount;
log_message("Fonka girdi");
//OPENING FILE**************************************************
AVFormatContext *pFormatCtx;
unsigned char r, g, b;
int i,j;

char* filename = "/sdcard/do-beer-not-drugs.3gp";
av_register_all();


// Open video file
if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)
return -1; // Couldn't open file

// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
return -1; // Couldn't find stream information

// Dump information about file onto standard error
dump_format(pFormatCtx, 0, filename, 0);
framecount = pFormatCtx->streams[0]->nb_frames;
hist = malloc(framecount*sizeof(int*));

for (j = 0; j < framecount; ++j) {
hist[j] = malloc(sizeof(int)*64); // this is because we use 64-bin histogram
}
for (i = 0; i < framecount; i++) {
for (j = 0; j < 64; j++) {
hist[j] = 0;
}

}

AVCodecContext *pCodecCtx;

// Find the first video stream
int videoStream;
videoStream=-1;
/*char info[40];
sprintf(info,"i degeri = %d",pFormatCtx->nb_streams);
log_message(info);*/
for(i=0; i<pFormatCtx->nb_streams; i++){
if(pFormatCtx->streams->codec->codec_type==CODEC_TYPE_VIDEO) {
videoStream=i;
break;
}}


if(videoStream==-1)
return -1; // Didn't find a video stream

AVCodec *pCodec;
char info[40];
sprintf(info,"i degeri = %d",pCodecCtx->codec_id);
log_message(info);

// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);


if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
log_message("here1");
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
return -1; // Could not open codec
log_message("here2");
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;

//STORING THE DATA************************************************

AVFrame *pFrame;

// Allocate video frame
pFrame=avcodec_alloc_frame();

// Allocate an AVFrame structure
AVFrame *pFrameRGB;
pFrameRGB=avcodec_alloc_frame();

if(pFrameRGB==NULL)
return -1;

uint8_t *buffer;
int numBytes;
// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height);

//READING DATA***************************************************

int frameFinished;
AVPacket packet;

i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,
packet.data, packet.size);

// Did we get a video frame?
if(frameFinished) {
static struct SwsContext *img_convert_ctx;

// Convert the image into RGB format
if(img_convert_ctx == NULL) {
int w = pCodecCtx->width;
int h = pCodecCtx->height;

img_convert_ctx = sws_getContext(w, h,
pCodecCtx->pix_fmt,
w, h, PIX_FMT_RGB24, SWS_BICUBIC,
NULL, NULL, NULL);
if(img_convert_ctx == NULL) {
fprintf(stderr, "Cannot initialize the conversion context!\n");
exit(1);
}
}
int ret = sws_scale(img_convert_ctx, pFrame->data, pFrame- >linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);

for (j = 0; j < 3*pCodecCtx->height*pCodecCtx->width -3; j++) {
r = (unsigned char) pFrameRGB->data[0][j];
g = (unsigned char) pFrameRGB->data[0][j+1];
b = (unsigned char) pFrameRGB->data[0][j+2];


r = (unsigned char) ((r >> 2) & 0x30);
g = (unsigned char) ((g >> 4) & 0x0C);
b = (unsigned char) ((b >> 6) & 0x03);

unsigned char h = (unsigned char)(r|g|b);
hist[h]++;
}

// Save the frame to sdcard
SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, ++i);
}
}

// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}



// Free the RGB image
av_free(buffer);
av_free(pFrameRGB);

// Free the YUV frame
av_free(pFrame);

// Close the codec
avcodec_close(pCodecCtx);

// Close the video file
av_close_input_file(pFormatCtx);


int keyframecount;
framecount=i;
keyframecount = select_keyFrames(framecount);
encodeVideo(env,keyframecount);
return 0;

}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones