void CFFMPEGLoader::SaveFrame(int iFrame, const char *add) { if(pFrame->linesize[0]==0) return; FILE *pFile; char szFilename[128]; int y; UINT numBytes=avpicture_get_size(PIX_FMT_RGB24, pVCodecCon->width, pVCodecCon->height)+100; uint8_t *buffer2=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); AVCodec* bmpCodec = avcodec_find_encoder(CODEC_ID_BMP); AVCodecContext* bmpCodecContext = avcodec_alloc_context(); avcodec_open(bmpCodecContext, bmpCodec); bmpCodecContext->height = pVCodecCon->height; bmpCodecContext->width = pVCodecCon->width; int encoded = bmpCodec->encode(bmpCodecContext, buffer2, numBytes, pFrame); avcodec_close(bmpCodecContext); // Open file sprintf(szFilename, "fr00000.bmp", add); UINT mul=10000,pos=2; while(mul>0) { szFilename[pos++]=iFrame/mul+'0'; iFrame%=mul; mul/=10; } string s=add; s+=szFilename; pFile=fopen(s.c_str(), "wb"); if(pFile==NULL) return; fwrite(buffer2, 1, encoded,pFile); // Close file fclose(pFile); av_free(buffer2); }
void CFFMPEGLoader::SaveFrame(const char *fn, int wanted_width, int wanted_height) { AVFrame *frameIN=NULL,*frameOUT=NULL; if(!pFrameRGB->data[1]) { frameOUT=pFrameRGB; frameIN=pFrame; } else { frameIN=pFrameRGB; frameOUT=pFrame; } if(frameIN->linesize[0]==0) return; FILE *pFile; char szFilename[128]; int y; if(wanted_width==0) wanted_width=pVCodecCon->width; if(wanted_height==0) wanted_height=pVCodecCon->height; CodecID id=CODEC_ID_BMP; int fmt=PIX_FMT_BGR24; { string str(fn),ending; if(str.find_last_of(".")!=-1) ending.append(str,str.find_last_of(".")+1,str.length()); for(int i=0; i<ending.length(); i++) ending[i] = tolower(ending[i]); if(ending.compare("jpg")==0||ending.compare("jpeg")==0) { id=CODEC_ID_JPEGLS; //don't work fmt=PIX_FMT_RGB32; } else if(ending.compare("gif")==0) { id=CODEC_ID_GIF; //don't work fmt=PIX_FMT_RGB32; } else if(ending.compare("tiff")==0) { id=CODEC_ID_TIFF; fmt=PIX_FMT_RGB32; } else if(ending.compare("png")==0) { id=CODEC_ID_PNG; fmt=PIX_FMT_RGB32; } } UINT numBytes=avpicture_get_size(fmt, wanted_width, wanted_height); uint8_t *buffer2=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); AVCodec* bmpCodec = avcodec_find_encoder(id); if(!bmpCodec) { av_free(buffer2); return; } AVCodecContext* bmpCodecContext = avcodec_alloc_context(); int a=avcodec_open(bmpCodecContext, bmpCodec); bmpCodecContext->pix_fmt=(PixelFormat)fmt; bmpCodecContext->height = wanted_height; bmpCodecContext->width = wanted_width; { uint8_t *buffer; // Determine required buffer size and allocate buffer numBytes=avpicture_get_size(fmt, wanted_width, wanted_height)+100; pBuffer=buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); avpicture_fill((AVPicture*)frameOUT, buffer, fmt, wanted_width, wanted_height); static struct SwsContext *img_convert_ctx; img_convert_ctx = sws_getContext(pVCodecCon->width, pVCodecCon->height, (int)pVCodecCon->pix_fmt, wanted_width, wanted_height, fmt, SWS_BICUBIC, NULL, NULL, NULL); if(img_convert_ctx == NULL) { cout<<"Cannot initialize the conversion context!\n"; return; } a=sws_scale(img_convert_ctx, frameIN->data, frameIN->linesize, 0, pVCodecCon->height, frameOUT->data, frameOUT->linesize); sws_freeContext(img_convert_ctx); } int encoded = bmpCodec->encode(bmpCodecContext, buffer2, numBytes, frameOUT); avcodec_close(bmpCodecContext); // Open file pFile=fopen(fn, "wb"); if(pFile==NULL) return; fwrite(buffer2, 1, encoded,pFile); // Close file fclose(pFile); av_free(buffer2); av_free(bmpCodecContext); }