2012-07-04 3 views
0

Я пытаюсь кодировать изображение в видео, используя библиотеку ffmpeg. У меня есть этот глобальный Params:Кодировать изображение в видео с помощью ffmpeg (sws_scale)

//Global params 
AVCodec   *codec; 
AVCodecContext *codecCtx; 
uint8_t   *output_buffer; 
int    output_buffer_size; 

Я разделил кодировку 3 способов: инициализировать кодер:

jint Java_com_camera_simpledoublewebcams2_CameraPreview_initencoder(JNIEnv* env,jobject thiz){ 
avcodec_register_all(); 
avcodec_init(); 
av_register_all(); 

int fps = 30; 

/* find the H263 video encoder */ 
codec = avcodec_find_encoder(CODEC_ID_H263); 
if (!codec) { 
    LOGI("avcodec_find_encoder() run fail."); 
    return -5; 
} 

//allocate context 
codecCtx = avcodec_alloc_context(); 

/* put sample parameters */ 
codecCtx->bit_rate = 400000; 
/* resolution must be a multiple of two */ 
codecCtx->width = 176; 
codecCtx->height = 144; 
/* frames per second */ 
codecCtx->time_base = (AVRational){1,fps}; 
codecCtx->pix_fmt = PIX_FMT_YUV420P; 
codecCtx->codec_id = CODEC_ID_H263; 
codecCtx->codec_type = AVMEDIA_TYPE_VIDEO; 

/* open it */ 
if (avcodec_open(codecCtx, codec) < 0) { 
    LOGI("avcodec_open() run fail."); 
    return -10; 
} 

//init buffer 
output_buffer_size = 500000; 
output_buffer = malloc(output_buffer_size); 

return 0; 

}

Кодирование изображения:

jint Java_com_camera_simpledoublewebcams2_CameraPreview_encodejpeg(JNIEnv* env,jobject thiz,jchar* cImage, jint imageSize){ 
int    out_size; 
AVFrame   *picture; 
AVFrame   *outpic; 
uint8_t   *outbuffer; 

//allocate frame  
picture = avcodec_alloc_frame();  
outpic = avcodec_alloc_frame(); 

int nbytes = avpicture_get_size(PIX_FMT_YUV420P, codecCtx->width, codecCtx->height); 
outbuffer = (uint8_t*)av_malloc(nbytes); 
outpic->pts = 0; 

//fill picture with image 
avpicture_fill((AVPicture*)picture, (uint8_t*)cImage, PIX_FMT_RGBA, codecCtx->width, codecCtx->height); 
//fill outpic with empty image 
avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, codecCtx->width, codecCtx->height); 

//rescale the image 
struct SwsContext* fooContext = sws_getContext(codecCtx->width, codecCtx->height, 
                 PIX_FMT_RGBA, 
                 codecCtx->width, codecCtx->height, 
                 PIX_FMT_YUV420P, 
                 SWS_FAST_BILINEAR, NULL, NULL, NULL); 
sws_scale(fooContext, picture->data, picture->linesize, 0, codecCtx->height, outpic->data, outpic->linesize); 

//encode the image 
out_size = avcodec_encode_video(codecCtx, output_buffer, output_buffer_size, outpic); 
out_size += avcodec_encode_video(codecCtx, output_buffer, output_buffer_size, outpic); 

//release pictures 
av_free(outbuffer); 
av_free(picture); 
av_free(outpic); 

return out_size; 

}

и закрытие кодировщик:

void Java_com_camera_simpledoublewebcams2_CameraPreview_closeencoder(JNIEnv* env,jobject thiz){ 
free(output_buffer); 
avcodec_close(codecCtx); 
av_free(codecCtx); 

}

Когда я посылаю первое изображение, я получаю результат от датчика. Когда я пытаюсь отправить другое изображение, программа выйдет из строя. Я попытался вызвать init один раз, а затем изображения, затем закрыть - не сработал. Я попытался вызвать init и закрыть для каждого изображения - не работал.

Любые предложения?

Спасибо!

EDIT: После дальнейших исследований я обнаружил, что проблема в методе sws_scale. До сих пор не знаю, что является причиной этой проблемы ...

+0

hai bahar_p сделал у вас ответ на этот вопрос. I также сталкивается с той же проблемой. Это мой почтовый идентификатор [email protected] – rams

ответ

1

out_size = avcodec_encode_video (codecCtx, output_buffer, output_buffer_size, outpic);

out_size + = avcodec_encode_video (codecCtx, output_buffer, output_buffer_size, outpic);

Почему вы кодируете дважды?

Возможно, ошибка связана с этой двойной кодировкой. Попробуйте удалить вторую кодировку.

Смежные вопросы