output_example h264 and mp4

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

output_example h264 and mp4

David REGADE
Hi,

I try to produce h264 encoded videos in mp4 container using libav* instead of
ffmpeg directly. I have compiled and tested output_example.c like that:

./output_example test.mp4

But it produces mp4 file with mp4 encoded video stream. So I have modified
output_example.c to force CCODEC_ID_H264 instead of default codec CODEC_ID_MPEG4:

.....
     /* allocate the output media context */
     oc = av_alloc_format_context();
     if (!oc) {
         fprintf(stderr, "Memory error\n");
         exit(1);
     }

     /* need h264 codec */
     fmt->video_codec = CODEC_ID_H264;
     oc->oformat = fmt;

     snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
.....

But when I launch output_example like above the program crash with a segfault.

Does anyone know what else I have modify in output_example to produce h264
encoded video stream in mp4 file format ?

Regards
--
David REGADE
_______________________________________________
libav-user mailing list
[hidden email]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: output_example h264 and mp4

Yohann Martineau-2
Le mardi 17 juin 2008 à 11:33 +0200, David REGADE a écrit :

> Hi,
>
> I try to produce h264 encoded videos in mp4 container using libav* instead of
> ffmpeg directly. I have compiled and tested output_example.c like that:
>
> ./output_example test.mp4
>
> But it produces mp4 file with mp4 encoded video stream. So I have modified
> output_example.c to force CCODEC_ID_H264 instead of default codec CODEC_ID_MPEG4:
>
> .....
>      /* allocate the output media context */
>      oc = av_alloc_format_context();
>      if (!oc) {
>          fprintf(stderr, "Memory error\n");
>          exit(1);
>      }
>
>      /* need h264 codec */
>      fmt->video_codec = CODEC_ID_H264;
>      oc->oformat = fmt;
>
>      snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
> .....
>
> But when I launch output_example like above the program crash with a segfault.
>
> Does anyone know what else I have modify in output_example to produce h264
> encoded video stream in mp4 file format ?
>

did you install x264? it's x264 which *encodes* to H264, not ffmpeg
directly.


_______________________________________________
libav-user mailing list
[hidden email]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: output_example h264 and mp4

David REGADE
Hi,

x264 is installed on my system. If I use output_example like that it works fine
but without the good container:

./output_example test.h264



Yohann Martineau a écrit :

> Le mardi 17 juin 2008 à 11:33 +0200, David REGADE a écrit :
>> Hi,
>>
>> I try to produce h264 encoded videos in mp4 container using libav* instead of
>> ffmpeg directly. I have compiled and tested output_example.c like that:
>>
>> ./output_example test.mp4
>>
>> But it produces mp4 file with mp4 encoded video stream. So I have modified
>> output_example.c to force CCODEC_ID_H264 instead of default codec CODEC_ID_MPEG4:
>>
>> .....
>>      /* allocate the output media context */
>>      oc = av_alloc_format_context();
>>      if (!oc) {
>>          fprintf(stderr, "Memory error\n");
>>          exit(1);
>>      }
>>
>>      /* need h264 codec */
>>      fmt->video_codec = CODEC_ID_H264;
>>      oc->oformat = fmt;
>>
>>      snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
>> .....
>>
>> But when I launch output_example like above the program crash with a segfault.
>>
>> Does anyone know what else I have modify in output_example to produce h264
>> encoded video stream in mp4 file format ?
>>
>
> did you install x264? it's x264 which *encodes* to H264, not ffmpeg
> directly.
>
>
> _______________________________________________
> libav-user mailing list
> [hidden email]
> https://lists.mplayerhq.hu/mailman/listinfo/libav-user

--
David REGADE
Ingénieur R&D / Chef de projet

Viewsurf S.A.S.
17 avenue des Mondaults
33270 Floirac - France
Tel: 05.57.22.54.40
Fax: 05.56.08.22.62
_______________________________________________
libav-user mailing list
[hidden email]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: output_example h264 and mp4

Luca Abeni
Hi David,

David REGADE wrote:
> Hi,
>
> x264 is installed on my system. If I use output_example like that it works fine
> but without the good container:

Try the attached patch: it should fix the problem.
I do not know if the problem is in output_example (well, it contains at least a
bug or two...) or in the mov muxer... I am going to check on ffmpeg-devel.


                                Luca

diff --git a/output_example.c b/output_example.c
index 515a172..65e09d0 100644
--- a/output_example.c
+++ b/output_example.c
@@ -156,7 +156,8 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
 
     pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
 
-    pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
+    if (c->coded_frame->pts != AV_NOPTS_VALUE)
+        pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
     pkt.flags |= PKT_FLAG_KEY;
     pkt.stream_index= st->index;
     pkt.data= audio_outbuf;
@@ -269,18 +270,6 @@ static void open_video(AVFormatContext *oc, AVStream *st)
         exit(1);
     }
 
-    video_outbuf = NULL;
-    if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
-        /* allocate output buffer */
-        /* XXX: API change will be done */
-        /* buffers passed into lav* can be allocated any way you prefer,
-           as long as they're aligned enough for the architecture, and
-           they're freed appropriately (such as using av_free for buffers
-           allocated with av_malloc) */
-        video_outbuf_size = 200000;
-        video_outbuf = av_malloc(video_outbuf_size);
-    }
-
     /* allocate the encoded raw picture */
     picture = alloc_picture(c->pix_fmt, c->width, c->height);
     if (!picture) {
@@ -373,6 +362,16 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
 
         ret = av_write_frame(oc, &pkt);
     } else {
+       video_outbuf = NULL;
+            /* allocate output buffer */
+            /* XXX: API change will be done */
+            /* buffers passed into lav* can be allocated any way you prefer,
+               as long as they're aligned enough for the architecture, and
+               they're freed appropriately (such as using av_free for buffers
+               allocated with av_malloc) */
+        video_outbuf_size = 200000;
+        video_outbuf = av_malloc(video_outbuf_size);
+
         /* encode the image */
         out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
         /* if zero size, it means the image was buffered */
@@ -380,7 +379,8 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
             AVPacket pkt;
             av_init_packet(&pkt);
 
-            pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
+            if (c->coded_frame->pts != AV_NOPTS_VALUE)
+                pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
             if(c->coded_frame->key_frame)
                 pkt.flags |= PKT_FLAG_KEY;
             pkt.stream_index= st->index;
@@ -389,6 +389,7 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
 
             /* write the compressed frame in the media file */
             ret = av_write_frame(oc, &pkt);
+            av_free(pkt.data);
         } else {
             ret = 0;
         }
@@ -409,7 +410,7 @@ static void close_video(AVFormatContext *oc, AVStream *st)
         av_free(tmp_picture->data[0]);
         av_free(tmp_picture);
     }
-    av_free(video_outbuf);
+//    av_free(video_outbuf);
 }
 
 /**************************************************************/
@@ -463,9 +464,11 @@ int main(int argc, char **argv)
        and initialize the codecs */
     video_st = NULL;
     audio_st = NULL;
+fmt->video_codec = CODEC_ID_H264;
     if (fmt->video_codec != CODEC_ID_NONE) {
         video_st = add_video_stream(oc, fmt->video_codec);
     }
+fmt->audio_codec = CODEC_ID_NONE;
     if (fmt->audio_codec != CODEC_ID_NONE) {
         audio_st = add_audio_stream(oc, fmt->audio_codec);
     }

_______________________________________________
libav-user mailing list
[hidden email]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user
Reply | Threaded
Open this post in threaded view
|

Re: output_example h264 and mp4

David REGADE
Hi Luca,

It works fine, thank you !!

Regards

Luca Abeni a écrit :

> Hi David,
>
> David REGADE wrote:
>> Hi,
>>
>> x264 is installed on my system. If I use output_example like that it
>> works fine but without the good container:
>
> Try the attached patch: it should fix the problem.
> I do not know if the problem is in output_example (well, it contains at
> least a
> bug or two...) or in the mov muxer... I am going to check on ffmpeg-devel.
>
>
>                 Luca
>
>
> ------------------------------------------------------------------------
>
> diff --git a/output_example.c b/output_example.c
> index 515a172..65e09d0 100644
> --- a/output_example.c
> +++ b/output_example.c
> @@ -156,7 +156,8 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
>  
>      pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
>  
> -    pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
> +    if (c->coded_frame->pts != AV_NOPTS_VALUE)
> +        pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
>      pkt.flags |= PKT_FLAG_KEY;
>      pkt.stream_index= st->index;
>      pkt.data= audio_outbuf;
> @@ -269,18 +270,6 @@ static void open_video(AVFormatContext *oc, AVStream *st)
>          exit(1);
>      }
>  
> -    video_outbuf = NULL;
> -    if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
> -        /* allocate output buffer */
> -        /* XXX: API change will be done */
> -        /* buffers passed into lav* can be allocated any way you prefer,
> -           as long as they're aligned enough for the architecture, and
> -           they're freed appropriately (such as using av_free for buffers
> -           allocated with av_malloc) */
> -        video_outbuf_size = 200000;
> -        video_outbuf = av_malloc(video_outbuf_size);
> -    }
> -
>      /* allocate the encoded raw picture */
>      picture = alloc_picture(c->pix_fmt, c->width, c->height);
>      if (!picture) {
> @@ -373,6 +362,16 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
>  
>          ret = av_write_frame(oc, &pkt);
>      } else {
> +       video_outbuf = NULL;
> +            /* allocate output buffer */
> +            /* XXX: API change will be done */
> +            /* buffers passed into lav* can be allocated any way you prefer,
> +               as long as they're aligned enough for the architecture, and
> +               they're freed appropriately (such as using av_free for buffers
> +               allocated with av_malloc) */
> +        video_outbuf_size = 200000;
> +        video_outbuf = av_malloc(video_outbuf_size);
> +
>          /* encode the image */
>          out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
>          /* if zero size, it means the image was buffered */
> @@ -380,7 +379,8 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
>              AVPacket pkt;
>              av_init_packet(&pkt);
>  
> -            pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
> +            if (c->coded_frame->pts != AV_NOPTS_VALUE)
> +                pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
>              if(c->coded_frame->key_frame)
>                  pkt.flags |= PKT_FLAG_KEY;
>              pkt.stream_index= st->index;
> @@ -389,6 +389,7 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
>  
>              /* write the compressed frame in the media file */
>              ret = av_write_frame(oc, &pkt);
> +            av_free(pkt.data);
>          } else {
>              ret = 0;
>          }
> @@ -409,7 +410,7 @@ static void close_video(AVFormatContext *oc, AVStream *st)
>          av_free(tmp_picture->data[0]);
>          av_free(tmp_picture);
>      }
> -    av_free(video_outbuf);
> +//    av_free(video_outbuf);
>  }
>  
>  /**************************************************************/
> @@ -463,9 +464,11 @@ int main(int argc, char **argv)
>         and initialize the codecs */
>      video_st = NULL;
>      audio_st = NULL;
> +fmt->video_codec = CODEC_ID_H264;
>      if (fmt->video_codec != CODEC_ID_NONE) {
>          video_st = add_video_stream(oc, fmt->video_codec);
>      }
> +fmt->audio_codec = CODEC_ID_NONE;
>      if (fmt->audio_codec != CODEC_ID_NONE) {
>          audio_st = add_audio_stream(oc, fmt->audio_codec);
>      }
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> libav-user mailing list
> [hidden email]
> https://lists.mplayerhq.hu/mailman/listinfo/libav-user

--
David REGADE
Ingénieur R&D / Chef de projet

Viewsurf S.A.S.
17 avenue des Mondaults
33270 Floirac - France
Tel: 05.57.22.54.40
Fax: 05.56.08.22.62
_______________________________________________
libav-user mailing list
[hidden email]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user