Good day to all.
I'm sorry, my english is not correct.
I'm trying to create a movie by reading a single image *.jpg in Linux
machine.
only one image ( is an exercise to understand )
I have now started to use the libraries
this is my code,
#include "avcodec.h"
#include "avformat.h"
#include "swscale.h"
#include "imgutils.h"
#include "opt.h"
#include "mathematics.h"
#include <stdio.h>
AVFrame *decode_image(const char *imagefilename,long * bufSize){
AVCodec *pCodec;
AVCodecContext *pCodecCtx = NULL;
AVFormatContext *pFormatCtx;
AVFrame *pFrame;
AVPacket packet;
uint8_t *outbuf;
int frameFinished,numBytes,ret; //out_size,i,;
pFormatCtx = avformat_alloc_context();
if( avformat_open_input(&pFormatCtx, imagefilename, NULL, NULL) !=0 )
{
printf("Can't open image file '%s'\n", imagefilename);
exit(1);
}
// data codec
pCodecCtx = pFormatCtx->streams[0]->codec;
pCodecCtx->width = 130;
pCodecCtx->height = 150;
pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
// find the mpeg4 video encoder
pCodec = avcodec_find_decoder( pCodecCtx->codec_id );
if (!pCodec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
// open it
if (avcodec_open(pCodecCtx, pCodec) < 0) {
fprintf(stderr, "could not open codec\n");
exit(1);
}
pFrame = avcodec_alloc_frame();
if ( !pFrame ){
printf("Can't allocate memory for AVFrame\n");
exit(1);
}
numBytes = avpicture_get_size(PIX_FMT_YUVJ420P, pCodecCtx->width,
pCodecCtx->height);
*bufSize = numBytes;
outbuf = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrame, outbuf, PIX_FMT_YUVJ420P,
pCodecCtx->width, pCodecCtx->height);
// Determine required buffer size and allocate buffer
while ( av_read_frame(pFormatCtx, &packet) >= 0 )
{
if(packet.stream_index != 0)
continue;
ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
&packet);
}
return pFrame;
}
int main(int argc, char** argv)
{
AVCodec *pCodec;
AVCodecContext *pCodecCtx = NULL;
AVFrame * frame;
long bufSize;
FILE *f;
int i,out_size;
const char *filename =
"/home/user/Lavoro/gestione_ffmpeg/prova_ffmpeg/prova.mp4";
const char *imagefilename =
"/home/user/Lavoro/gestione_ffmpeg/prova_ffmpeg/ora_02.jpg";
avcodec_init();
avcodec_register_all();
av_register_all();
printf("image deconding \n");
frame = decode_image(imagefilename,&bufSize);
pCodec = avcodec_find_encoder(CODEC_ID_MPEG4);
if (!pCodec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
// pCodecCtx->codec_id = CODEC_ID_MPEG4;
pCodecCtx = avcodec_alloc_context();
pCodecCtx->bit_rate = 400000;
pCodecCtx->width = 130;
pCodecCtx->height = 150;
pCodecCtx->time_base = (AVRational){1,5};
pCodecCtx->max_b_frames=1;
pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
/* open it */
if (avcodec_open(pCodecCtx, pCodec) < 0) {
fprintf(stderr, "could not open codec\n");
exit(1);
}
f = fopen(filename,"w");
if ( !f ){
fprintf(stderr,"could not open codec \n");
exit(1);
}
uint8_t * picBuf = (uint8_t *)malloc(bufSize);
for(i=0;i<30;i++){
out_size = avcodec_encode_video(pCodecCtx, picBuf, bufSize, frame);
printf("encoding frame %3d (size=%5d)\n", i, out_size);
fwrite(picBuf, 1, out_size, f);
}
// get the delayed frames
for(; out_size; i++) {
fflush(stdout);
out_size = avcodec_encode_video(pCodecCtx, picBuf, bufSize, NULL);
printf("write frame %3d (size=%5d)\n", i, out_size);
fwrite(picBuf, 1, out_size, f);
}
/* add sequence end code to have a real mpeg file */
picBuf[0] = 0x00;
picBuf[1] = 0x00;
picBuf[2] = 0x01;
picBuf[3] = 0xb7;
fwrite(picBuf, 1, 4, f);
fclose(f);
free(picBuf);
avcodec_close(pCodecCtx);
av_free(pCodecCtx);
av_free(frame);
printf("\n");
return 0;
}
the code create the movie but the screen is black
where is the problem ?....
can anyone help me ,
and tell me where wrong, so to better understand the use of the library
thank you very much
--
Paolo Melucci
_______________________________________________
libav-api mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-api