[Libav-user] [External] (no subject)

Niraj Gandha niraj.gandha at einfochips.com
Mon Sep 24 22:48:41 EEST 2018


Use this code Jimmy Bhaktha....

Code for the issue and getting rgb frames from raw H.264 Frame  is as follows:

avcodec_register_all(); av_register_all(); avformat_network_init();
AVDictionary *options = NULL;
AVFormatContext *refrenceFormatCtx = NULL;
AVInputFormat *fmts = av_find_input_format("h264");
char errorsdef[100];
AVCodecContext* codec_ctx = NULL;
int video_stream_index = 0;
SwsContext *img_convert_ctx = NULL;
AVFrame* picture_yuv = NULL;
AVFrame* picture_rgb = NULL;
uint8_t* picture_buffer_rgb;
uint8_t *rgb_image_data;
int sizeofrgbpicture = 0;
int initialize_rgb_requirements=1;
picture_yuv = av_frame_alloc();
av_dict_set(&options, "flags", "bicubic", 0); av_opt_set(refrenceFormatCtx,"f","h264", AV_OPT_SEARCH_CHILDREN); av_opt_set(refrenceFormatCtx,"codec:v","h264",AV_OPT_SEARCH_CHILDREN);
av_opt_set(refrenceFormatCtx,"probesize","32M", AV_OPT_SEARCH_CHILDREN); // Open video file int err =
avformat_open_input(&refrenceFormatCtx,"tcp://192.168.42.129:2226", fmts, &options);
if (!options) {
int dict_count = av_dict_count(options);
qDebug() << "dict_count " << dict_count;
}
av_strerror(err,errorsdef,100);
qDebug() << "OPening Stream error: "<< err << " "<< errorsdef;
if (refrenceFormatCtx!=NULL){
err = avformat_find_stream_info(refrenceFormatCtx, &options);
if( err< 0){
av_strerror(err,errorsdef,100);
qDebug() << "Not able to find stream: "<< err << " "<< errorsdef;
     }
}else{
qDebug() << "referencecontext null";
exit(1);
} //search video stream
for (int i = 0; i < (int)refrenceFormatCtx->nb_streams; i++)
{
AVStream* s = refrenceFormatCtx->streams[i];
if (s->codec == NULL){
continue;
}
codec_ctx = (s->codec);
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO){
video_stream_index = i;
}
}
AVPacket packet; av_init_packet(&packet); //open output file
AVFormatContext* output_ctx = avformat_alloc_context();
AVStream* stream = NULL; //start reading packets from stream and emit data pointer to slot
av_read_play(refrenceFormatCtx); //play RTSP
avcodec_copy_context(codec_ctx, refrenceFormatCtx->streams[video_stream_index]->codec);
if (avcodec_open2(codec_ctx, avcodec_find_decoder(AV_CODEC_ID_H264), NULL) < 0){
qDebug() << "avcodec_open2 null";
} while (av_read_frame(refrenceFormatCtx, &packet) >= 0) {
if (packet.stream_index == video_stream_index) { //packet is video
if (stream == NULL) { //create stream in file
stream = avformat_new_stream(output_ctx, refrenceFormatCtx->streams[video_stream_index]->codec->codec);
avcodec_copy_context(stream->codec, refrenceFormatCtx->streams[video_stream_index]->codec);
stream->sample_aspect_ratio = refrenceFormatCtx->streams[video_stream_index]->codec->sample_aspect_ratio;
} int check = 0; packet.stream_index = stream->id; int result = av
codec_decode_video2(codec_ctx, picture_yuv, &check, &packet);
av_free_packet(&packet);
av_packet_unref(&packet);
if(result <= 0 || check == 0){
continue;
}
if(initialize_rgb_requirements) {
sizeofrgbpicture = avpicture_get_size(AV_PIX_FMT_RGB24, codec_ctx->width, codec_ctx->height);
picture_rgb = av_frame_alloc();
picture_buffer_rgb = (uint8_t*) (av_malloc(sizeofrgbpicture));
avpicture_fill((AVPicture *) picture_rgb, picture_buffer_rgb, AV_PIX_FMT_RGB24, codec_ctx->width, codec_ctx->height);
img_convert_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUV420P, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); initialize_rgb_requirements=0;
}
int height = 0;
if(picture_yuv->data != NULL) { height = sws_scale(img_convert_ctx, ((AVPicture*)picture_yuv)->data, ((AVPicture*)picture_yuv)->linesize, 0, codec_ctx->height, ((AVPicture*)picture_rgb)->data,((AVPicture*)picture_rgb)->linesize);
}
rgb_image_data = (uint8_t *)malloc(sizeofrgbpicture * sizeof(uint8_t));
int ret = avpicture_layout((AVPicture *)picture_rgb, AV_PIX_FMT_RGB24, codec_ctx->width, codec_ctx->height, rgb_image_data, sizeofrgbpicture);
emit imageQueued(rgb_image_data, codec_ctx->width,codec_ctx->height);
}
msleep(1);
}
av_freep(picture_buffer_rgb);
av_frame_free(&picture_rgb);
avio_close(output_ctx->pb);
avformat_free_context(output_ctx); avformat_close_input(&refrenceFormatCtx);

we have to tell ffmpeg that the format is h264. For that I have used AVInputFormat, to set other options like video codec and probesize, I have used av_op_set(). To set the default flags in ffmpeg, I have used av_dict_set(). I have emitted the data pointer to my required slot. If any one wants to create a file from it, then it can generate .ppm file by writing this pointer into file.





Thanks & Regards,

Niraj Gandha

Trainee Engineer | PES





Tel: 7016116093 | Cell: 9408781216 | Ext.: 3318

Product Engineering Services

Software | System | Silicon | Mechanical

<http://www.einfochips.com/>
www.einfochips.com
 |
niraj.gandha at einfochips.com<mailto:niraj.gandha at einfochips.com>



































20 Years of Engineering Innovation & Excellence
Recognized as 'Leader' in Zinnov's Global Service Providers Rating-2015

From: Libav-user <libav-user-bounces at ffmpeg.org> on behalf of Jimmy Bhaktha <jimmy at accelrobotics.com>
Sent: Tuesday, September 25, 2018 12:55:49 AM
To: libav-user at ffmpeg.org
Subject: [External] [Libav-user] (no subject)

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

Hi

I am working on an application based on FFMPEG version 3.4.2 where the RTSP packets from the camera are captured and store using python. The decoder application is run later.  The application receives a  byte array which is the actual packet. The goal is to decode the received packet and convert it to RGB from YUV.

When I feed it packets I see the following output

Creating the codec h264
Creating the codec context
[h264 @ 0x557a3846d880] nal_unit_type: 7, nal_ref_idc: 3
[h264 @ 0x557a3846d880] nal_unit_type: 8, nal_ref_idc: 3
[h264 @ 0x557a3846d880] nal_unit_type: 5, nal_ref_idc: 3
[h264 @ 0x557a3846d880] Reinit context to 2688x1520, pix_fmt: yuvj420p
avcodec_send_packet=0
avcodec_receive_frame=0, pCodecCtx->frame_number=1
[swscaler @ 0x557a385fdb20] bad src image pointers
avcodec_send_packet=0
avcodec_receive_frame=0, pCodecCtx->frame_number=2
[swscaler @ 0x557a385fdb20] bad src image pointers

I was  wondering why the swscaler is complaining about bad src images.

Here is the sequence of calls in my code

1.  av_packet_alloc() and av_packet_init()
     - copy the encoded image to packet->data (I extend it by AV_INPUT_BUFFER_PADDING_SIZE and set the memory to zero
2.  av_register_all();
3.  Create the codec and Codec Context for h264 and open it.
    - I Copy the extra data from the encoded frame to this context.
4. Allocate the memory for the frames (YUV and RGB)
5. First time using the coded I call decode - I see the codec context now has the correct values for height and width.
    - For decoding I use the avcodec_send_packet and check in a while loop for avcodec_receive_frame() I see the number of decoded frames in the codec context goes up (pCodecCtx->frame_number)
6. Use this codec context to set up the sws_context
7. Use the sws_context and the YUV frame decoded to get the RGB frame.


--
Jimmy Bhaktha
************************************************************************************************************************************************************* eInfochips Business Disclaimer: This e-mail message and all attachments transmitted with it are intended solely for the use of the addressee and may contain legally privileged and confidential information. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution, copying, or other use of this message or its attachments is strictly prohibited. If you have received this message in error, please notify the sender immediately by replying to this message and please delete it from your computer. Any views expressed in this message are those of the individual sender unless otherwise stated. Company has taken enough precautions to prevent the spread of viruses. However the company accepts no liability for any damage caused by any virus transmitted by this email. *************************************************************************************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20180924/a8ea36ab/attachment.html>


More information about the Libav-user mailing list