The encoded raw data (nal[] units) can decode using ffmpeg library. You have to initialize the AVCodecContext there and pass each raw encoded-data to avcodec_decode_video2() method.<br><br><div class="gmail_quote">On Mon, Feb 4, 2013 at 2:18 PM, Imran Khan <span dir="ltr"><<a href="mailto:imrank@cdac.in" target="_blank">imrank@cdac.in</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello All<br>
<br>
I am trying to decode raw H264 frames using libavcodec. After getting<br>
frames from webcam I am encoding it using H264 codec. After that I am<br>
passing these frames one by one to a H264 decoder. In decoder<br>
AVCodecContext I am setting width,height and pixel format only. Problem<br>
is decoder not decompressing the frames. the size of the frame are same as<br>
the encoded frame size. Can anyone tell me what may be the problem.<br>
<br>
Here is my decoder code...<br>
<br>
#include "video_decoder.h"<br>
<br>
using namespace std;<br>
<br>
uint8_t* rgb_bytes;<br>
<br>
AVFrame *alloc_dec_picture(enum PixelFormat pix_fmt, int width, int<br>
height){<br>
<br>
AVFrame *picture;<br>
<br>
uint8_t *picture_buf;<br>
<br>
int size;<br>
picture = avcodec_alloc_frame();<br>
<br>
if (!picture)<br>
return NULL;<br>
<br>
size = avpicture_get_size(pix_fmt, width, height);<br>
<br>
picture_buf = (uint8_t*)av_malloc(size);<br>
<br>
if (!picture_buf) {<br>
<br>
av_free(picture);<br>
<br>
return NULL;<br>
<br>
}<br>
<br>
avpicture_fill((AVPicture *)picture, picture_buf,pix_fmt, width,<br>
height);<br>
<br>
return picture;<br>
<br>
}<br>
<br>
VideoDecoder::VideoDecoder(){<br>
<br>
avcodec_register_all();<br>
<br>
avctx = NULL;<br>
<br>
picture = NULL;<br>
<br>
frame = NULL;<br>
<br>
}<br>
<br>
<br>
char* VideoDecoder::decoder_init(AVCodecID codec_id){<br>
<br>
int error;<br>
<br>
AVCodec* codec;<br>
<br>
av_init_packet(&avpkt);<br>
<br>
codec = avcodec_find_decoder(codec_id);<br>
<br>
if(!codec){<br>
<br>
return "codec not found";<br>
<br>
<br>
}<br>
<br>
avctx = avcodec_alloc_context3(codec);<br>
<br>
if(!avctx){<br>
<br>
return "can not allocate avcontext :: avocdec_alloc_context";<br>
<br>
}<br>
<br>
avctx->width=640;<br>
<br>
avctx->height=480;<br>
<br>
avctx->pix_fmt=AV_PIX_FMT_YUV420P;<br>
<br>
if(codec->capabilities&CODEC_CAP_TRUNCATED)<br>
avctx->flags|= CODEC_FLAG_TRUNCATED;<br>
<br>
error = avcodec_open2(avctx ,codec ,NULL);<br>
<br>
if(error < 0 ){<br>
<br>
return "can not open codec :: avcodec_open2";<br>
<br>
}<br>
<br>
frame = avcodec_alloc_frame();<br>
<br>
picture = alloc_dec_picture(PIX_FMT_RGB24, 640, 480);<br>
<br>
return "decoder init completed\n";<br>
}<br>
<br>
char* VideoDecoder::decode_frame(uint8_t *frame_buffer, size_t<br>
data_length){<br>
<br>
int w = 640;<br>
<br>
int h = 480;<br>
<br>
rgb_bytes =NULL;<br>
<br>
int memBytes = avpicture_get_size(PIX_FMT_RGB24 , avctx->width ,<br>
avctx->height );<br>
<br>
int len ,got_frame;<br>
<br>
avpkt.size = data_length;<br>
<br>
avpkt.data = frame_buffer;<br>
<br>
if(!frame_buffer){<br>
<br>
return "frame buffer empty\n";<br>
<br>
}<br>
<br>
len = avcodec_decode_video2(avctx ,frame ,&got_frame ,&avpkt);<br>
<br>
if( len < 0){<br>
<br>
return "error while decoding\n";<br>
<br>
}<br>
<br>
if( got_frame ){<br>
<br>
static struct SwsContext *img_convert_ctx;<br>
<br>
if(img_convert_ctx == NULL) {<br>
<br>
img_convert_ctx = sws_getContext(w, h,<br>
PIX_FMT_YUV420P, avctx->width,<br>
avctx->height, PIX_FMT_BGR24,<br>
SWS_BICUBIC, NULL, NULL, NULL);<br>
<br>
if(img_convert_ctx == NULL) {<br>
<br>
return "Cannot initialize the conversion context!\n";<br>
<br>
}<br>
<br>
}<br>
<br>
sws_scale(img_convert_ctx,<br>
frame->data , frame->linesize ,<br>
0, h ,picture->data,<br>
picture->linesize );<br>
<br>
rgb_bytes = new uint8_t[memBytes];<br>
<br>
memcpy(rgb_bytes ,picture->data ,memBytes);<br>
<br>
}<br>
<br>
rgb_bytes = new uint8_t[memBytes];<br>
<br>
memcpy(rgb_bytes ,frame->data ,memBytes);<br>
<br>
<br>
av_free_packet(&avpkt);<br>
<br>
return "completed decoding\n";<br>
}<br>
<br>
<br>
--<br>
Thanks and Regards<br>
Imran Khan<br>
<br>
<br>
<br>
-------------------------------------------------------------------------------------------------------------------------------<br>
<br>
This e-mail is for the sole use of the intended recipient(s) and may<br>
contain confidential and privileged information. If you are not the<br>
intended recipient, please contact the sender by reply e-mail and destroy<br>
all copies and the original message. Any unauthorized review, use,<br>
disclosure, dissemination, forwarding, printing or copying of this email<br>
is strictly prohibited and appropriate legal action will be taken.<br>
-------------------------------------------------------------------------------------------------------------------------------<br>
<br>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
</blockquote></div><br>