static int DecodeFrame(const char* lpszOutFileName, AVCodecContext* pDecCtx, AVFrame* pFrame, AVFrame* pFrameRGB, int* pnFrameCount, AVPacket* pAVPacket, int bLastFrame) { int nGotFrame = 0; int nLen = avcodec_decode_video2(pDecCtx, pFrame, &nGotFrame, pAVPacket); if (nLen < 0) { fprintf(stderr, "Error while decoding frame %d\n", *pnFrameCount); return nLen; } if (nGotFrame) { printf("Saving %sframe %3d\n", bLastFrame ? "last " : "", *pnFrameCount); fflush(stdout); #if 0 char buf[1024]; /* the picture is allocated by the decoder, no need to free it */ snprintf(buf, sizeof(buf), outfilename, *pnFrameCount); SavePGM(pFrame->data[0], pFrame->linesize[0], pDecCtx->width, pDecCtx->height, buf); #else //yuv420p to rgb if (pFrameBuffer == NULL) { int numBytes = avpicture_get_size(PIX_FMT_RGB24, pDecCtx->width, pDecCtx->height); pFrameBuffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t)); avpicture_fill((AVPicture *)pFrameRGB, pFrameBuffer, PIX_FMT_RGB24, pDecCtx->width, pDecCtx->height); } SwsContext* pImgConvertCtx = NULL; pImgConvertCtx = sws_getCachedContext(pImgConvertCtx, pDecCtx->width, pDecCtx->height, pDecCtx->pix_fmt, pDecCtx->width, pDecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); sws_scale(pImgConvertCtx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pDecCtx->height, pFrameRGB->data, pFrameRGB->linesize); //save to file SavePPM(pFrameRGB, pDecCtx->width, pDecCtx->height, *pnFrameCount); SaveYUV420P(pFrame, pDecCtx->width, pDecCtx->height, lpszOutFileName); #endif (*pnFrameCount)++; } if (pAVPacket->data) { pAVPacket->size -= nLen; pAVPacket->data += nLen; } return 0; }
// // Save the image to a file (PPM, JPEG and PNG // formats are supported). // Returns a non-zero value on error. // STStatus STImage::Save(const std::string& filename) const { // Determine the right routine based on the file's extension. // The format-specific subroutines are each implemented in // a different file. std::string ext = STGetExtension( filename ); if (ext.compare("PPM") == 0 ) { return SavePPM(filename); } else if (ext.compare("PNG") == 0) { return SavePNG(filename); } else if (ext.compare("JPG") == 0) { return SaveJPG(filename); } else { fprintf(stderr, "STImage::Save() - Unknown image file type \"%s\".\n", filename.c_str()); assert(false); return ST_ERROR; } }