JNIEXPORT jint JNICALL Java_org_jitsi_impl_neomedia_codec_FFmpeg_avcodec_1encode_1video (JNIEnv *env, jclass clazz, jlong ctx, jbyteArray buf, jint buf_size, jlong frame) { jint ret; if (buf) { jbyte *buf_ = (*env)->GetByteArrayElements(env, buf, NULL); if (buf_) { ret = (jint) avcodec_encode_video( (AVCodecContext *) (intptr_t) ctx, (uint8_t *) buf_, (int) buf_size, (const AVFrame *) (intptr_t) frame); (*env)->ReleaseByteArrayElements(env, buf, buf_, 0); } else { ret = -1; } } else { ret = -1; } return ret; }
static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){ mp_image_t* dmpi; int out_size; AVFrame *pic= vf->priv->pic; pic->data[0]=mpi->planes[0]; pic->data[1]=mpi->planes[1]; pic->data[2]=mpi->planes[2]; pic->linesize[0]=mpi->stride[0]; pic->linesize[1]=mpi->stride[1]; pic->linesize[2]=mpi->stride[2]; out_size = avcodec_encode_video(&lavc_venc_context, vf->priv->outbuf, vf->priv->outbuf_size, pic); if(out_size<=0) return 1; dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES, MP_IMGTYPE_EXPORT, 0, mpi->w, mpi->h); vf->priv->pes.data=vf->priv->outbuf; vf->priv->pes.size=out_size; vf->priv->pes.id=0x1E0; vf->priv->pes.timestamp=-1; // dunno dmpi->planes[0]=(unsigned char*)&vf->priv->pes; return vf_next_put_image(vf,dmpi, MP_NOPTS_VALUE); }
// private method int FFmpegEncoder::encodeVideoData(AVPicture *picture, FFmpegVideoParam &picParam) { AVCodecContext *videoCodecContext = this->videoStream->codec; AVFrame *frame = avcodec_alloc_frame(); if (!frame) { LOGE("FFmpegEncoder::encodeVideoData, fail to avcodec_alloc_frame"); return -1; } // convert the pixel format if needed if (picParam.pixelFormat != videoCodecContext->pix_fmt || picParam.width != videoCodecContext->width || picParam.height != videoCodecContext->height) { av_free(frame); LOGE("FFmpegEncoder::encodeVideoData, VideoParam match error"); return -1; } else { // fill the frame *(AVPicture *)frame = *picture; } // encode the frame frame->pts = AV_NOPTS_VALUE; int encodedSize = 0; encodedSize = avcodec_encode_video(videoCodecContext, this->videoBuffer, this->videoBufferSize, frame); av_free(frame); return (encodedSize < 0) ? -1 : encodedSize; }
long FFmpegVideo::SaveFrame2jpeg (AVCodecContext *pCodecCtxIn, AVFrame *pFrame, char *fileName) { uint8_t *Buffer=0; int BufSiz=0; int BufSizActua=0l; int ImgFmt = PIX_FMT_YUVJ420P; //for the newer ffmpeg version, this int to pixelformat FILE *JPEGFile=0; pFrame->pts = 1; pFrame->quality = pCodecCtxIn->global_quality; BufSiz = avpicture_get_size ( PIX_FMT_BGR24, pCodecCtxIn->width, pCodecCtxIn->height ); Buffer = (uint8_t *)malloc ( BufSiz ); if ( Buffer == NULL ) return 0; memset ( Buffer, 0, BufSiz ); long BufSizActual = avcodec_encode_video( pCodecCtx, Buffer, BufSiz, pFrame ); JPEGFile = fopen ( fileName, "wb" ); fwrite ( Buffer, 1, BufSizActual, JPEGFile ); fclose ( JPEGFile ); free ( Buffer ); return BufSizActual; }
void capture_end(capture* c) { for(; c->out_size; c->i++) { c->out_size = avcodec_encode_video(c->c, c->outbuf, c->outbuf_size, NULL); printf("write frame %3d (size=%5d)\n", c->i, c->out_size); fwrite(c->outbuf, 1, c->out_size, c->f); } c->outbuf[0] = 0x00; c->outbuf[1] = 0x00; c->outbuf[2] = 0x01; c->outbuf[3] = 0xb7; fwrite(c->outbuf, 1, 4, c->f); fclose(c->f); free(c->picture_buf); free(c->picture_buf_rgb); free(c->outbuf); av_free(c->c); av_free(c->picture); av_free(c->picture_rgb); avcodec_close(c->c); printf("\n"); free(c); }
bool VideoEncoder::encode(const AVFrame* decodedData, AVPacket& encodedData) { // Be sure that data of AVPacket is NULL so that the encoder will allocate it encodedData.data = NULL; AVCodecContext& avCodecContext = _codec.getAVCodecContext(); #if LIBAVCODEC_VERSION_MAJOR > 53 int gotPacket = 0; const int ret = avcodec_encode_video2(&avCodecContext, &encodedData, decodedData, &gotPacket); if(ret != 0) { throw std::runtime_error("Encode video frame error: avcodec encode video frame - " + getDescriptionFromErrorCode(ret)); } return gotPacket == 1; #else const int ret = avcodec_encode_video(&avCodecContext, encodedData.data, encodedData.size, decodedData); if(ret < 0) { throw std::runtime_error("Encode video frame error: avcodec encode video frame - " + getDescriptionFromErrorCode(ret)); } return true; #endif }
void VideoWriterThread::writeFrame(AVFrame* pFrame) { ScopeTimer timer(ProfilingZoneWriteFrame); m_FramesWritten++; AVCodecContext* pCodecContext = m_pVideoStream->codec; int out_size = avcodec_encode_video(pCodecContext, m_pVideoBuffer, VIDEO_BUFFER_SIZE, pFrame); /* if zero size, it means the image was buffered */ if (out_size > 0) { AVPacket packet; av_init_packet(&packet); if ((pCodecContext->coded_frame->pts) != (long long)AV_NOPTS_VALUE) { packet.pts = av_rescale_q(pCodecContext->coded_frame->pts, pCodecContext->time_base, m_pVideoStream->time_base); } if (pCodecContext->coded_frame->key_frame) { #if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(52, 31, 0) packet.flags |= AV_PKT_FLAG_KEY; #else packet.flags |= PKT_FLAG_KEY; #endif } packet.stream_index = m_pVideoStream->index; packet.data = m_pVideoBuffer; packet.size = out_size; /* write the compressed frame in the media file */ int ret = av_interleaved_write_frame(m_pOutputFormatContext, &packet); AVG_ASSERT(ret == 0); } }
bool VideoIO::writeFrame(void) { int w = pOutputCodecCtx->width; int h = pOutputCodecCtx->height; //get frame Frame *frame = NULL; while(frame == NULL) { if(pOutputFrameHeap->isEmpty() == false) { ///TODO frame = pOutputFrameHeap->pop(); break; //get output frame : success } } //convert RGB24->YUV402P RGB24ToYUV420P(frame->getFrame(), w, h); //write frame outSize = avcodec_encode_video(pOutputCodecCtx, pOutBuffer, outBufferSize, pOutputFrame); printf("[VideoIO] write Frame : id#%d, size : %d\n", frame->getId(), outSize); fwrite(pOutBuffer, 1, outSize, pOutputFile); //saveFrame(frame, w, h, frame->getId()); //push frame to unused output frame stack pUnusedOutputFrameStack->push(frame); return true; }
static int encode(struct iaxc_video_codec *c, int inlen, char * in, struct slice_set_t * slice_set) { struct encoder_ctx *e = (struct encoder_ctx *) c->encstate; int encoded_size; avcodec_get_frame_defaults(e->picture); e->picture->data[0] = (unsigned char *)in; e->picture->data[1] = (unsigned char *)in + e->avctx->width * e->avctx->height; e->picture->data[2] = (unsigned char *)in + e->avctx->width * e->avctx->height * 5 / 4; e->picture->linesize[0] = e->avctx->width; e->picture->linesize[1] = e->avctx->width / 2; e->picture->linesize[2] = e->avctx->width / 2; /* TODO: investigate setting a real pts value */ e->picture->pts = AV_NOPTS_VALUE; /* TODO: investigate quality */ e->picture->quality = 10; g_slice_set = slice_set; slice_set->num_slices = 0; encoded_size = avcodec_encode_video(e->avctx, e->frame_buf, e->frame_buf_len, e->picture); if (!encoded_size) { fprintf(stderr, "codec_ffmpeg: encode failed\n"); return -1; } slice_set->key_frame = e->avctx->coded_frame->key_frame; /* This is paranoia, of course. */ g_slice_set = 0; /* We are in one of two modes here. * * The first possibility is that the codec supports rtp * packetization. In this case, the slice_set has already been * filled via encode_rtp_callback() calls made during the call * to avcodec_encode_video(). * * The second possibility is that we have one big encoded frame * that we need to slice-up ourselves. */ if (!e->avctx->rtp_payload_size) slice_encoded_frame(&e->slice_header, slice_set, e->frame_buf, encoded_size, c->fragsize); return 0; }
const unsigned char* OStreamVideoEncoder::encode_video_frame( IplImage *image, int *out_size ) { AVCodecContext *codec_context; *out_size = 0; if ( image == NULL ) return NULL; if ( !_initialized ) { // DEBUG_PRINT( "initializing class failed.\n" ); return NULL; } if( image->imageSize > _internal_buffer_size ) { // DEBUG_PRINT( "image_size too big.\n" ); return NULL; } if ( _opencv_frame ==NULL ) { _opencv_frame = create_avframe( _opencv_pix_fmt,image->width,image->height ); } codec_context = _video_stream->codec; // this is an hack. We just assign the pointer image->imageData to // _opencv_frame->data[0] without using a memcpy. This should speed the things up. _opencv_frame->data[0] = (uint8_t*)image->imageData; avpicture_fill((AVPicture *)_opencv_frame,(uint8_t*)image->imageData,_opencv_pix_fmt,image->width,image->height); if (sws_ctx==NULL){ sws_ctx=sws_getContext(image->width,image->height, _opencv_pix_fmt,codec_context->width,codec_context->height, codec_context->pix_fmt,SWS_FAST_BILINEAR,NULL, NULL, NULL); } long len = sws_scale(sws_ctx,((AVPicture *)_opencv_frame)->data,((AVPicture *)_opencv_frame)->linesize, 0, image->height, ((AVPicture *)_ready_to_encode_frame)->data, ((AVPicture *)_ready_to_encode_frame)->linesize); /*/ int img_convert(AVPicture *dst, int dst_pix_fmt, const AVPicture *src, int pix_fmt, int width, int height); /*/ // img_convert( (AVPicture *)_ready_to_encode_frame, codec_context->pix_fmt, // (AVPicture *)_opencv_frame, _opencv_pix_fmt, codec_context->width, // codec_context->height ); /*/ int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVFrame *pict); /*/ // encode the image *out_size = avcodec_encode_video( codec_context, _internal_buffer, _internal_buffer_size, _ready_to_encode_frame ); return _internal_buffer; }
/*-------------------------------------------------------------------------*/ int FFV1_Compress(unsigned char *in,unsigned char *out,uint32_t *outlen) { uint32_t sz; unsigned char *src; if(codec==FF_XVID) { return FFV1_XvidCompress(in,out,outlen); } #ifdef CONVERT_YUV static char tmp[720*576*3]; #endif _frame.linesize[0]=_w; _frame.linesize[1]=_w>>1; _frame.linesize[2]=_w>>1; _frame.pts = AV_NOPTS_VALUE; _frame.quality=vinfo.quality; // printf("\n in Qual : %f \n",_frame.quality); #ifdef CONVERT_YUV src=tmp; YU_YV12_mmx(in,tmp, _w,_h); #else src=in; #endif _frame.data[0]=(uint8_t *)src; _frame.data[1]=(uint8_t *)src+pagesize; _frame.data[2]=(uint8_t *)src+pagesize+(pagesize>>2); if(nextIsKF) { _frame.key_frame = 1; _frame.pict_type=FF_I_TYPE; nextIsKF=0; } else { _frame.pict_type=0; _frame.key_frame = 0; } if( (sz=avcodec_encode_video( _context, (uint8_t *)out, _w*_h*3, &_frame))<0) { printf("error encoding !\n"); exit(3); } //printf("\n out Qual : %f \n",_context->coded_frame->quality); *outlen=sz; return 1; }
int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st, uint8_t * outbuf, uint32_t outbuf_size, AVFrame * picture ){ CV_FUNCNAME("icv_av_write_frame_FFMPEG"); #if LIBAVFORMAT_BUILD > 4628 AVCodecContext * c = video_st->codec; #else AVCodecContext * c = &(video_st->codec); #endif int out_size; int ret; __BEGIN__; if (oc->oformat->flags & AVFMT_RAWPICTURE) { /* raw video case. The API will change slightly in the near futur for that */ AVPacket pkt; av_init_packet(&pkt); pkt.flags |= PKT_FLAG_KEY; pkt.stream_index= video_st->index; pkt.data= (uint8_t *)picture; pkt.size= sizeof(AVPicture); ret = av_write_frame(oc, &pkt); } else { /* encode the image */ out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture); /* if zero size, it means the image was buffered */ if (out_size > 0) { AVPacket pkt; av_init_packet(&pkt); #if LIBAVFORMAT_BUILD > 4752 pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base); #else pkt.pts = c->coded_frame->pts; #endif if(c->coded_frame->key_frame) pkt.flags |= PKT_FLAG_KEY; pkt.stream_index= video_st->index; pkt.data= outbuf; pkt.size= out_size; /* write the compressed frame in the media file */ ret = av_write_frame(oc, &pkt); } else { ret = 0; } } if (ret != 0) { CV_ERROR(CV_StsError, "Error while writing video frame"); } __END__; return CV_StsOk; }
static int write_lavc(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp) { void *outbuffer = NULL; int success = 0; AVFrame *pic = NULL; struct AVCodec *codec = avcodec_find_encoder(ctx->writer->lavc_codec); AVCodecContext *avctx = NULL; if (!codec) goto print_open_fail; avctx = avcodec_alloc_context3(codec); if (!avctx) goto print_open_fail; avctx->time_base = AV_TIME_BASE_Q; avctx->width = image->w; avctx->height = image->h; avctx->pix_fmt = imgfmt2pixfmt(image->imgfmt); if (ctx->writer->lavc_codec == CODEC_ID_PNG) avctx->compression_level = ctx->opts->png_compression; if (avcodec_open2(avctx, codec, NULL) < 0) { print_open_fail: mp_msg(MSGT_CPLAYER, MSGL_INFO, "Could not open libavcodec encoder" " for saving images\n"); goto error_exit; } size_t outbuffer_size = image->w * image->h * 3 * 2; outbuffer = malloc(outbuffer_size); if (!outbuffer) goto error_exit; pic = avcodec_alloc_frame(); if (!pic) goto error_exit; avcodec_get_frame_defaults(pic); for (int n = 0; n < 4; n++) { pic->data[n] = image->planes[n]; pic->linesize[n] = image->stride[n]; } int size = avcodec_encode_video(avctx, outbuffer, outbuffer_size, pic); if (size < 1) goto error_exit; fwrite(outbuffer, size, 1, fp); success = 1; error_exit: if (avctx) avcodec_close(avctx); av_free(avctx); avcodec_free_frame(&pic); free(outbuffer); return success; }
/** This function is used to process the input buffer and provide one output buffer */ void omx_videoenc_component_BufferMgmtCallback(OMX_COMPONENTTYPE *openmaxStandComp, OMX_BUFFERHEADERTYPE* pInputBuffer, OMX_BUFFERHEADERTYPE* pOutputBuffer) { omx_videoenc_component_PrivateType* omx_videoenc_component_Private = openmaxStandComp->pComponentPrivate; omx_base_video_PortType *inPort = (omx_base_video_PortType *)omx_videoenc_component_Private->ports[OMX_BASE_FILTER_INPUTPORT_INDEX]; OMX_S32 nOutputFilled = 0; OMX_U8* outputCurrBuffer; OMX_S32 nLen = 0; int size; size= inPort->sPortParam.format.video.nFrameWidth*inPort->sPortParam.format.video.nFrameHeight; DEBUG(DEB_LEV_FUNCTION_NAME, "In %s\n", __func__); /** Fill up the current input buffer when a new buffer has arrived */ if(omx_videoenc_component_Private->isNewBuffer) { omx_videoenc_component_Private->isNewBuffer = 0; DEBUG(DEB_LEV_FULL_SEQ, "New Buffer FilledLen = %d\n", (int)pInputBuffer->nFilledLen); omx_videoenc_component_Private->picture->data[0] = pInputBuffer->pBuffer; omx_videoenc_component_Private->picture->data[1] = omx_videoenc_component_Private->picture->data[0] + size; omx_videoenc_component_Private->picture->data[2] = omx_videoenc_component_Private->picture->data[1] + size / 4; omx_videoenc_component_Private->picture->linesize[0] = inPort->sPortParam.format.video.nFrameWidth; omx_videoenc_component_Private->picture->linesize[1] = inPort->sPortParam.format.video.nFrameWidth / 2; omx_videoenc_component_Private->picture->linesize[2] = inPort->sPortParam.format.video.nFrameWidth / 2; } outputCurrBuffer = pOutputBuffer->pBuffer; pOutputBuffer->nFilledLen = 0; pOutputBuffer->nOffset = 0; while (!nOutputFilled) { if (omx_videoenc_component_Private->isFirstBuffer) { tsem_down(omx_videoenc_component_Private->avCodecSyncSem); omx_videoenc_component_Private->isFirstBuffer = 0; } omx_videoenc_component_Private->avCodecContext->frame_number++; nLen = avcodec_encode_video(omx_videoenc_component_Private->avCodecContext, outputCurrBuffer, pOutputBuffer->nAllocLen, omx_videoenc_component_Private->picture); if (nLen < 0) { DEBUG(DEB_LEV_ERR, "----> A general error or simply frame not encoded?\n"); } pInputBuffer->nFilledLen = 0; omx_videoenc_component_Private->isNewBuffer = 1; if ( nLen >= 0) { pOutputBuffer->nFilledLen = nLen; } nOutputFilled = 1; } DEBUG(DEB_LEV_FULL_SEQ, "One output buffer %x nLen=%d is full returning in video encoder\n", (int)pOutputBuffer->pBuffer, (int)pOutputBuffer->nFilledLen); }
uint8_t ffmpegEncoder::encode(ADMImage *in,ADMBitstream *out) { int32_t sz = 0; ADM_assert(out->bufferSize); encodePreamble (in->data); if ((sz = avcodec_encode_video (_context, out->data, out->bufferSize, &_frame)) < 0) return 0; postAmble(out,sz); return 1; }
bool CFfmpegVideoEncoder::EncodeImage( const u_int8_t* pY, const u_int8_t* pU, const u_int8_t* pV, u_int32_t yStride, u_int32_t uvStride, bool wantKeyFrame, Duration elapsedDuration, Timestamp srcFrameTimestamp) { m_push->Push(srcFrameTimestamp); if (m_vopBuffer == NULL) { m_vopBuffer = (u_int8_t*)malloc(Profile()->m_videoMaxVopSize); if (m_vopBuffer == NULL) { return false; } } if (m_media_frame == H263VIDEOFRAME) { m_count++; if (m_count >= m_key_frame_count) { wantKeyFrame = true; m_count = 0; } } if (wantKeyFrame) m_picture->pict_type = FF_I_TYPE; //m_picture->key_frame = 1; else //m_picture->key_frame = 0; m_picture->pict_type = 0; m_picture->data[0] = (uint8_t *)pY; m_picture->data[1] = (uint8_t *)pU; m_picture->data[2] = (uint8_t *)pV; m_picture->linesize[0] = yStride; m_picture->linesize[1] = uvStride; m_picture->linesize[2] = uvStride; m_picture->pts = srcFrameTimestamp; #if 0 if (m_picture->key_frame == 1) { debug_message("key frame "U64, srcFrameTimestamp); } #endif m_vopBufferLength = avcodec_encode_video(m_avctx, m_vopBuffer, Profile()->m_videoMaxVopSize, m_picture); //debug_message(U64" ffmpeg len %d", srcFrameTimestamp, m_vopBufferLength); #ifdef OUTPUT_RAW if (m_vopBufferLength) { fwrite(m_vopBuffer, m_vopBufferLength, 1, m_outfile); } #endif // m_avctx.frame_number++; return true; }
static bool encode_video(ffemu_t *handle, AVPacket *pkt, AVFrame *frame) { av_init_packet(pkt); pkt->data = handle->video.outbuf; pkt->size = handle->video.outbuf_size; #ifdef HAVE_FFMPEG_AVCODEC_ENCODE_VIDEO2 int got_packet = 0; if (avcodec_encode_video2(handle->video.codec, pkt, frame, &got_packet) < 0) return false; if (!got_packet) { pkt->size = 0; pkt->pts = AV_NOPTS_VALUE; pkt->dts = AV_NOPTS_VALUE; return true; } if (pkt->pts != (int64_t)AV_NOPTS_VALUE) { pkt->pts = av_rescale_q(pkt->pts, handle->video.codec->time_base, handle->muxer.vstream->time_base); } if (pkt->dts != (int64_t)AV_NOPTS_VALUE) { pkt->dts = av_rescale_q(pkt->dts, handle->video.codec->time_base, handle->muxer.vstream->time_base); } #else int outsize = avcodec_encode_video(handle->video.codec, handle->video.outbuf, handle->video.outbuf_size, frame); if (outsize < 0) return false; pkt->size = outsize; if (handle->video.codec->coded_frame->pts != (int64_t)AV_NOPTS_VALUE) { pkt->pts = av_rescale_q(handle->video.codec->coded_frame->pts, handle->video.codec->time_base, handle->muxer.vstream->time_base); } else pkt->pts = AV_NOPTS_VALUE; if (handle->video.codec->coded_frame->key_frame) pkt->flags |= AV_PKT_FLAG_KEY; #endif pkt->stream_index = handle->muxer.vstream->index; return true; }
inline int icv_av_write_frame_FFMPEG( AVFormatContext * oc, AVStream * video_st, uint8_t * outbuf, uint32_t outbuf_size, AVFrame * picture ) { #if LIBAVFORMAT_BUILD > 4628 AVCodecContext * c = video_st->codec; #else AVCodecContext * c = &(video_st->codec); #endif int out_size; int ret = 0; if (oc->oformat->flags & AVFMT_RAWPICTURE) { /* raw video case. The API will change slightly in the near futur for that */ AVPacket pkt; av_init_packet(&pkt); #ifndef PKT_FLAG_KEY #define PKT_FLAG_KEY AV_PKT_FLAG_KEY #endif pkt.flags |= PKT_FLAG_KEY; pkt.stream_index= video_st->index; pkt.data= (uint8_t *)picture; pkt.size= sizeof(AVPicture); ret = av_write_frame(oc, &pkt); } else { /* encode the image */ out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture); /* if zero size, it means the image was buffered */ if (out_size > 0) { AVPacket pkt; av_init_packet(&pkt); #if LIBAVFORMAT_BUILD > 4752 if(c->coded_frame->pts != (s64)AV_NOPTS_VALUE_) pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base); #else pkt.pts = c->coded_frame->pts; #endif if(c->coded_frame->key_frame) pkt.flags |= PKT_FLAG_KEY; pkt.stream_index= video_st->index; pkt.data= outbuf; pkt.size= out_size; /* write the compressed frame in the media file */ ret = av_write_frame(oc, &pkt); } else { ret = OPENCV_NO_FRAMES_WRITTEN_CODE; } } return ret; }
int FFMpegEncoder::encodeVideoFrame(AVFrame *decodedFrame,unsigned char* encodeBuf,int encodeBufSize) { if (pVideoCodecCtx->me_threshold == 0) decodedFrame->pict_type = 0; videoEncodedSize = avcodec_encode_video(pVideoCodecCtx,encodeBuf,encodeBufSize, decodedFrame); if (videoEncodedSize < 0) errinfo("encode error!"); return videoEncodedSize; }
VideoIO::~VideoIO() { //close output video if(outSize != -1) { //get the delayed frames for(int i = getFrameIndex() ; outSize ; i++) { fflush(stdout); outSize = avcodec_encode_video(pOutputCodecCtx, pOutBuffer, outBufferSize, NULL); printf("[VideoIO] write frame : %3d (size=%5d)\n", i, outSize); fwrite(pOutBuffer, 1, outSize, pOutputFile); } //add sequence end code to have a real mpeg file pOutBuffer[0] = 0x00; pOutBuffer[1] = 0x00; pOutBuffer[2] = 0x01; pOutBuffer[3] = 0xb7; fwrite(pOutBuffer, 1, 4, pOutputFile); } //free the rgb image if(pInputFrame != NULL) av_free(pInputFrame); if(pOutputFrame != NULL) { free(pOutputFrame->data[0]); av_free(pOutputFrame); } //close the codec if(pOutputCodecCtx != NULL) { avcodec_close(pOutputCodecCtx); av_free(pOutputCodecCtx); fclose(pOutputFile); //output video } if(pInputCodecCtx != NULL) { avcodec_close(pInputCodecCtx); } //close input video if(pFormatCtx != NULL) { av_close_input_file(pFormatCtx); } if(pOutBuffer != NULL) free(pOutBuffer); }
int RealFFMpegCodecEncoder::EncodeFrame(FFMpegFrame *pFrame) { for (int i=0;i<3;i++) { frameSrc->data[i] = (uint8_t*)pFrame->data[i]; frameSrc->linesize[i] = pFrame->linesize[i]; } int out_size = avcodec_encode_video(c, (uint8_t*)encBuf, encBufSize, frameSrc); printf("codec_frame=%d,type=%d\n",c->coded_frame->coded_picture_number,c->coded_frame->pict_type); frameSrc->pts++; return out_size; }
int avcodec_encode_video2 (AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr) { int error=avcodec_encode_video(avctx, avpkt->data, avpkt->size,frame); if (error<0){ return error; }else{ if (error>0) { *got_packet_ptr=1; avpkt->size=error; }else *got_packet_ptr=0; } return 0; }
/* Encodes and writes a video frame using the av_write_frame API. This is * a helper function for ffmpeg_put_image and ffmpeg_put_other_image. */ void ffmpeg_put_frame(struct ffmpeg *ffmpeg, AVFrame *pic) { int out_size, ret; #ifdef FFMPEG_AVWRITEFRAME_NEWAPI AVPacket pkt; av_init_packet(&pkt); /* init static structure */ pkt.stream_index = ffmpeg->video_st->index; #endif /* FFMPEG_AVWRITEFRAME_NEWAPI */ if (ffmpeg->oc->oformat->flags & AVFMT_RAWPICTURE) { /* raw video case. The API will change slightly in the near future for that */ #ifdef FFMPEG_AVWRITEFRAME_NEWAPI pkt.flags |= PKT_FLAG_KEY; pkt.data = (uint8_t *)pic; pkt.size = sizeof(AVPicture); ret = av_write_frame(ffmpeg->oc, &pkt); #else ret = av_write_frame(ffmpeg->oc, ffmpeg->video_st->index, (uint8_t *)pic, sizeof(AVPicture)); #endif /* FFMPEG_AVWRITEFRAME_NEWAPI */ } else { /* encode the image */ out_size = avcodec_encode_video(AVSTREAM_CODEC_PTR(ffmpeg->video_st), ffmpeg->video_outbuf, ffmpeg->video_outbuf_size, pic); /* if zero size, it means the image was buffered */ if (out_size != 0) { /* write the compressed frame in the media file */ /* XXX: in case of B frames, the pts is not yet valid */ #ifdef FFMPEG_AVWRITEFRAME_NEWAPI pkt.pts = AVSTREAM_CODEC_PTR(ffmpeg->video_st)->coded_frame->pts; if (AVSTREAM_CODEC_PTR(ffmpeg->video_st)->coded_frame->key_frame) { pkt.flags |= PKT_FLAG_KEY; } pkt.data = ffmpeg->video_outbuf; pkt.size = out_size; ret = av_write_frame(ffmpeg->oc, &pkt); #else ret = av_write_frame(ffmpeg->oc, ffmpeg->video_st->index, ffmpeg->video_outbuf, out_size); #endif /* FFMPEG_AVWRITEFRAME_NEWAPI */ } else { ret = 0; } } if (ret != 0) { motion_log(LOG_ERR, 1, "Error while writing video frame"); return; } }
Codec_Errors H263VideoEncoder::Encode(void* inSample, long insize, void** outSample, long* outsize, long long timestamp){ Codec_Errors retval = CODEC_SUCCEEDED; try{ VideoMediaFormat* vf = (VideoMediaFormat*) CurrentFormat; if(vf == NULL){ //if there is no format information, then we haven't opened yet. retval = CODEC_CODEC_NOT_OPENED; } else if(FFEncoderContext == NULL){ //if the context isn't instantiated, then we haven't opened yet. retval = CODEC_CODEC_NOT_OPENED; } else if(FFEncoder == NULL) //if the encoder isn't instantiated, then we haven't been opened yet. { retval = CODEC_CODEC_NOT_OPENED; } else{ //get the FFMpeg equiv. pixel format for the format provided. PixelFormat fmt = (PixelFormat)VideoMediaFormat::GetFFPixel(vf->PixelFormat); //if the format of the incoming frame isn't what the encoder expects, then we need to scale. if(fmt != PIX_FMT_YUV420P){ AVFrame* tpic = alloc_and_fill_picture(fmt, vf->Width, vf->Height, inSample); sws_scale(ScaleContext, tpic->data, tpic->linesize, 0, vf->Height, TempFrame->data, TempFrame->linesize); av_free(tpic); tpic = NULL; } else{ //if there is no need to scale, then copy the incoming frame direct. memcpy(TempFrame->data, inSample, insize); } //create a buffer to receive the compressed frame, with a little padding for good measure. //TO-DO: Is this size necessary or beneficial? int outbuf_size = 100000 + 12*vf->Width*vf->Height; void* outbuf = av_malloc(outbuf_size); //encode the video frame. outbuf_size = avcodec_encode_video(FFEncoderContext, (unsigned char*)outbuf, outbuf_size, TempFrame); //if the resulting buffer size is 0, then we didn't get anything back from the function. //This isn't bad, just means the encoder needs some data to start encoding. if(outbuf_size == 0){ av_free(outbuf); //free the buffer } else{ *outSample = outbuf; //set the reference for the outgoing sample. } *outsize = outbuf_size; //set the size of the outgoing sample. } } catch(...){ retval = CODEC_UNEXPECTED; } return retval; }
void VideoWriterThread::writeFrame(AVFrame* pFrame) { ScopeTimer timer(ProfilingZoneWriteFrame); m_FramesWritten++; AVCodecContext* pCodecContext = m_pVideoStream->codec; AVPacket packet = { 0 }; int ret; bool bGotOutput; #if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(54, 0, 0) av_init_packet(&packet); int got_output = 0; ret = avcodec_encode_video2(pCodecContext, &packet, pFrame, &got_output); AVG_ASSERT(ret >= 0); if ((pCodecContext->coded_frame->pts) != (long long)AV_NOPTS_VALUE) { packet.pts = av_rescale_q(pCodecContext->coded_frame->pts, pCodecContext->time_base, m_pVideoStream->time_base); } bGotOutput = (got_output != 0); #else int out_size = avcodec_encode_video(pCodecContext, m_pVideoBuffer, VIDEO_BUFFER_SIZE, pFrame); if (out_size > 0) { av_init_packet(&packet); if ((pCodecContext->coded_frame->pts) != (long long)AV_NOPTS_VALUE) { packet.pts = av_rescale_q(pCodecContext->coded_frame->pts, pCodecContext->time_base, m_pVideoStream->time_base); } if (pCodecContext->coded_frame->key_frame) { packet.flags |= AV_PKT_FLAG_KEY; } packet.stream_index = m_pVideoStream->index; packet.data = m_pVideoBuffer; packet.size = out_size; } bGotOutput = (out_size > 0); #endif if (bGotOutput) { /* write the compressed frame in the media file */ ret = av_interleaved_write_frame(m_pOutputFormatContext, &packet); av_free_packet(&packet); if (ret != 0) { AVG_TRACE(Logger::category::VIDEO, Logger::severity::ERROR, getAVErrorString(ret)); } AVG_ASSERT(ret == 0); } }
void AVIDump::AddFrame(const u8* data, int width, int height) { avpicture_fill((AVPicture *)s_BGRFrame, const_cast<u8*>(data), PIX_FMT_BGR24, width, height); // Convert image from BGR24 to desired pixel format, and scale to initial // width and height struct SwsContext *s_SwsContext; if ((s_SwsContext = sws_getContext(width, height, PIX_FMT_BGR24, s_width, s_height, s_Stream->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr))) { sws_scale(s_SwsContext, s_BGRFrame->data, s_BGRFrame->linesize, 0, height, s_YUVFrame->data, s_YUVFrame->linesize); sws_freeContext(s_SwsContext); } // Encode and write the image int outsize = avcodec_encode_video(s_Stream->codec, s_OutBuffer, s_size, s_YUVFrame); while (outsize > 0) { AVPacket pkt; av_init_packet(&pkt); if (s_Stream->codec->coded_frame->pts != (unsigned int)AV_NOPTS_VALUE) pkt.pts = av_rescale_q(s_Stream->codec->coded_frame->pts, s_Stream->codec->time_base, s_Stream->time_base); if (s_Stream->codec->coded_frame->key_frame) pkt.flags |= AV_PKT_FLAG_KEY; pkt.stream_index = s_Stream->index; pkt.data = s_OutBuffer; pkt.size = outsize; // Write the compressed frame in the media file av_interleaved_write_frame(s_FormatContext, &pkt); // Encode delayed frames outsize = avcodec_encode_video(s_Stream->codec, s_OutBuffer, s_size, nullptr); } }
void VideoWriter::newFrame(QVideoFrame frame) { if ((frame.width() != width) || (frame.height() != height)) { width = frame.width(); height = frame.height(); } if (waitingToInitialize) { initialize(*vFilename); } if (currentlyWriting) { if (!frame.map(QAbstractVideoBuffer::ReadOnly)) { qDebug() << "Failure to map video frame in writer"; return; } AVCodecContext *c = video_st->codec; avpicture_fill((AVPicture *)tmp_picture, frame.bits(), PIX_FMT_RGB24, c->width, c->height); sws_scale(sws_ctx, tmp_picture->data, tmp_picture->linesize, 0, c->height, picture->data, picture->linesize); picture->pts = frameCounter++; frame.unmap(); /* encode the image */ /* if zero size, it means the image was buffered */ /* write the compressed frame in the media file */ /* XXX: in case of B frames, the pts is not yet valid */ int out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture); if (out_size > 0) { AVPacket pkt; av_init_packet(&pkt); if (c->coded_frame->pts != AV_NOPTS_VALUE) pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base); if(c->coded_frame->key_frame) pkt.flags |= AV_PKT_FLAG_KEY; pkt.stream_index= video_st->index; pkt.data= video_outbuf; pkt.size= out_size; /* write the compressed frame in the media file */ //ret = av_interleaved_write_frame(oc, &pkt); int ret = av_write_frame(oc, &pkt); } // Save time stamp } }
void imageEncoder::encodeImage(char* inImage, int inImageSize, int counter, char* outImage, int *outImageSize) { if (!fooContext) { LOGI(LOG_LEVEL, "Not initialized, so cannot encode"); exit(1); } 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 = counter; //fill picture with image avpicture_fill((AVPicture*) picture, (uint8_t*) inImage, PIX_FMT_YUV420P, codecCtx->width, codecCtx->height); //fill outpic with empty image avpicture_fill((AVPicture*) outpic, outbuffer, PIX_FMT_YUV420P, codecCtx->width, codecCtx->height); //rescale the image 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); // write to a outImage for (int i = 0; i < (int) output_buffer_size; i++) outImage[i] = (char) output_buffer[i]; *outImageSize = (int) output_buffer_size; //release pictures av_free(outbuffer); av_free(picture); av_free(outpic); }
/*********************** * EncodeFrame * Codifica un frame ************************/ VideoFrame* H263Encoder1996::EncodeFrame(BYTE *in,DWORD len) { //Check we are opened if (!opened) //Error return NULL; //Get number of pixels in image int numPixels = ctx->width*ctx->height; //Comprobamos el tama�o if (numPixels*3/2 != len) //Error return NULL; //POnemos los valores picture->data[0] = in; picture->data[1] = in+numPixels; picture->data[2] = in+numPixels*5/4; //Clean all previous packets frame->ClearRTPPacketizationInfo(); //Codificamos DWORD bufLen = avcodec_encode_video(ctx,frame->GetData(),frame->GetMaxMediaLength(),picture); //Set length frame->SetLength(bufLen); //Set width and height frame->SetWidth(ctx->width); frame->SetHeight(ctx->height); //Is intra frame->SetIntra(ctx->coded_frame->key_frame); //Unset fpu picture->key_frame = 0; picture->pict_type = AV_PICTURE_TYPE_NONE; //Paquetize paquetizer.PaquetizeFrame(frame); //From the first num = 0; return frame; }
static PyObject* Codec_Encode( PyCodecObject* obj, PyObject *args) { //#ifndef WIN32 PyVFrameObject* cFrame = NULL; PyObject* cRes = NULL; int iLen = 0; AVFrame picture; #define ENCODE_OUTBUF_SIZE 300000 char sOutbuf[ ENCODE_OUTBUF_SIZE ]; if (!PyArg_ParseTuple(args, "O!", &VFrameType, &cFrame )) return NULL; if (!(obj->cCodec ||obj->cCodec->codec)) { PyErr_SetString(g_cErr, "Encode error:codec not initialized" ); return NULL; } //reset codec parameters if frame size is smaller than codec frame size if (!obj->cCodec->width || ! obj->cCodec->height) { PyErr_SetString(g_cErr, "Encode: zero frame size set in codec" ); return NULL; } if ((obj->cCodec->width > frame_get_width(cFrame)) || (obj->cCodec->height > frame_get_height(cFrame)) ) { PyErr_SetString(g_cErr, "Encode: cannot change resolution for frame. Use scaling first..." ); return NULL; } /* check codec params */ PyVFrame2AVFrame(cFrame, &picture, 1 ); iLen = avcodec_encode_video(obj->cCodec, sOutbuf, ENCODE_OUTBUF_SIZE, &picture); if (iLen > 0) cRes= Frame_New_LAVC_Enc( obj, sOutbuf, iLen ); else PyErr_Format(g_cErr, "Failed to encode frame( error code is %d )", iLen ); #ifdef HAVE_MMX emms(); #endif return cRes; }