diff mbox

[FFmpeg-devel] avformat/movenc: allow rewriting extradata

Message ID CABSox7hz5jtgZspAZN-0yqyrJdH_9cKFK2uHnXVOE92t3rK-Hg@mail.gmail.com
State Accepted
Headers show

Commit Message

Umair Khan Aug. 19, 2016, 11:05 a.m. UTC
Hi,

On Fri, Aug 19, 2016 at 4:46 AM, Michael Niedermayer
<michael@niedermayer.cc> wrote:
> On Thu, Aug 18, 2016 at 11:21:00AM +0530, Umair Khan wrote:
>> Hi,
>>
>> On Thu, Aug 18, 2016 at 2:10 AM, Umair Khan <omerjerk@gmail.com> wrote:
>> > On Thu, Aug 18, 2016 at 1:28 AM, James Almer <jamrial@gmail.com> wrote:
>> >> On 8/17/2016 3:59 PM, Umair Khan wrote:
>> >>> Hi,
>> >>>
>> >>> Patch attached.
>> >>>
>> >>> I hope this is the cleanest solution. :)
>> >>>
>> >>> - Umair
>> >>>
>> >>>
>> >>> patch.diff
>> >>>
>> >>>
>> >>> From be04357d54897173b1776ed92ab8347b3cdffd46 Mon Sep 17 00:00:00 2001
>> >>> From: Umair Khan <omerjerk@gmail.com>
>> >>> Date: Thu, 18 Aug 2016 00:27:42 +0530
>> >>> Subject: [PATCH] avformat/movenc: allow rewriting extradata
>> >>>
>> >>> Signed-off-by: Umair Khan <omerjerk@gmail.com>
>> >>> ---
>> >>>  libavformat/movenc.c | 30 ++++++++++++++++++++++++++++++
>> >>>  libavformat/movenc.h |  2 ++
>> >>>  2 files changed, 32 insertions(+)
>> >>>
>> >>> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
>> >>> index 14db880..b33f24c 100644
>> >>> --- a/libavformat/movenc.c
>> >>> +++ b/libavformat/movenc.c
>> >>> @@ -4919,6 +4919,19 @@ static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
>> >>>                      trk->start_cts = pkt->pts - pkt->dts;
>> >>>                  else
>> >>>                      trk->start_cts = 0;
>> >>> +            }
>> >>> +
>> >>> +            if (trk->par->codec_id == AV_CODEC_ID_MP4ALS) {
>> >>> +                int side_size = 0;
>> >>> +                uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
>> >>> +                av_log(s, AV_LOG_ERROR, "side_size = %d\n", side_size);
>> >>
>> >> I'm not sure this is useful at all, but if you think it is
>> >> then make it either AV_ERROR_VERBOSE or AV_ERROR_DEBUG.
>> >
>> > Sorry for this. I overlooked it.
>> > I'll resend the patch.
>>
>> Updated patch attached.
>>
>> - Umair
>
>>  movenc.c |   28 ++++++++++++++++++++++++++++
>>  movenc.h |    2 ++
>>  2 files changed, 30 insertions(+)
>> 0b95f020ea5f06493a88680a686398eddcd954de  patch.diff
>> From 3bda0c5d727e7b1f45e8fddc8c0022c0a1d5e66a Mon Sep 17 00:00:00 2001
>> From: Umair Khan <omerjerk@gmail.com>
>> Date: Thu, 18 Aug 2016 00:27:42 +0530
>> Subject: [PATCH] avformat/movenc: allow rewriting extradata
>>
>> Signed-off-by: Umair Khan <omerjerk@gmail.com>
>> ---
>>  libavformat/movenc.c | 28 ++++++++++++++++++++++++++++
>>  libavformat/movenc.h |  2 ++
>>  2 files changed, 30 insertions(+)
>>
>> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
>> index 14db880..1235b37 100644
>> --- a/libavformat/movenc.c
>> +++ b/libavformat/movenc.c
>> @@ -4919,6 +4919,18 @@ static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
>>                      trk->start_cts = pkt->pts - pkt->dts;
>>                  else
>>                      trk->start_cts = 0;
>> +            }
>> +
>> +            if (trk->par->codec_id == AV_CODEC_ID_MP4ALS) {
>> +                int side_size = 0;
>> +                uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
>> +                if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
>> +                    av_free(par->extradata);
>
>> +                    par->extradata = av_mallocz(side_size + AV_INPUT_BUFFER_PADDING_SIZE);
>> +                    memcpy(par->extradata, side, side_size);
>
> missing check for malloc failure

Updated patch attached.

- Umair
From 57cf341487672f60e0304ebc863f6cae5e55e676 Mon Sep 17 00:00:00 2001
From: Umair Khan <omerjerk@gmail.com>
Date: Thu, 18 Aug 2016 00:27:42 +0530
Subject: [PATCH] avformat/movenc: allow rewriting extradata

Signed-off-by: Umair Khan <omerjerk@gmail.com>
---
 libavformat/movenc.c | 31 +++++++++++++++++++++++++++++++
 libavformat/movenc.h |  2 ++
 2 files changed, 33 insertions(+)

Comments

Michael Niedermayer Aug. 19, 2016, 1:59 p.m. UTC | #1
On Fri, Aug 19, 2016 at 04:35:59PM +0530, Umair Khan wrote:
> Hi,
> 
> On Fri, Aug 19, 2016 at 4:46 AM, Michael Niedermayer
> <michael@niedermayer.cc> wrote:
> > On Thu, Aug 18, 2016 at 11:21:00AM +0530, Umair Khan wrote:
> >> Hi,
> >>
> >> On Thu, Aug 18, 2016 at 2:10 AM, Umair Khan <omerjerk@gmail.com> wrote:
> >> > On Thu, Aug 18, 2016 at 1:28 AM, James Almer <jamrial@gmail.com> wrote:
> >> >> On 8/17/2016 3:59 PM, Umair Khan wrote:
> >> >>> Hi,
> >> >>>
> >> >>> Patch attached.
> >> >>>
> >> >>> I hope this is the cleanest solution. :)
> >> >>>
> >> >>> - Umair
> >> >>>
> >> >>>
> >> >>> patch.diff
> >> >>>
> >> >>>
> >> >>> From be04357d54897173b1776ed92ab8347b3cdffd46 Mon Sep 17 00:00:00 2001
> >> >>> From: Umair Khan <omerjerk@gmail.com>
> >> >>> Date: Thu, 18 Aug 2016 00:27:42 +0530
> >> >>> Subject: [PATCH] avformat/movenc: allow rewriting extradata
> >> >>>
> >> >>> Signed-off-by: Umair Khan <omerjerk@gmail.com>
> >> >>> ---
> >> >>>  libavformat/movenc.c | 30 ++++++++++++++++++++++++++++++
> >> >>>  libavformat/movenc.h |  2 ++
> >> >>>  2 files changed, 32 insertions(+)
> >> >>>
> >> >>> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
> >> >>> index 14db880..b33f24c 100644
> >> >>> --- a/libavformat/movenc.c
> >> >>> +++ b/libavformat/movenc.c
> >> >>> @@ -4919,6 +4919,19 @@ static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
> >> >>>                      trk->start_cts = pkt->pts - pkt->dts;
> >> >>>                  else
> >> >>>                      trk->start_cts = 0;
> >> >>> +            }
> >> >>> +
> >> >>> +            if (trk->par->codec_id == AV_CODEC_ID_MP4ALS) {
> >> >>> +                int side_size = 0;
> >> >>> +                uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
> >> >>> +                av_log(s, AV_LOG_ERROR, "side_size = %d\n", side_size);
> >> >>
> >> >> I'm not sure this is useful at all, but if you think it is
> >> >> then make it either AV_ERROR_VERBOSE or AV_ERROR_DEBUG.
> >> >
> >> > Sorry for this. I overlooked it.
> >> > I'll resend the patch.
> >>
> >> Updated patch attached.
> >>
> >> - Umair
> >
> >>  movenc.c |   28 ++++++++++++++++++++++++++++
> >>  movenc.h |    2 ++
> >>  2 files changed, 30 insertions(+)
> >> 0b95f020ea5f06493a88680a686398eddcd954de  patch.diff
> >> From 3bda0c5d727e7b1f45e8fddc8c0022c0a1d5e66a Mon Sep 17 00:00:00 2001
> >> From: Umair Khan <omerjerk@gmail.com>
> >> Date: Thu, 18 Aug 2016 00:27:42 +0530
> >> Subject: [PATCH] avformat/movenc: allow rewriting extradata
> >>
> >> Signed-off-by: Umair Khan <omerjerk@gmail.com>
> >> ---
> >>  libavformat/movenc.c | 28 ++++++++++++++++++++++++++++
> >>  libavformat/movenc.h |  2 ++
> >>  2 files changed, 30 insertions(+)
> >>
> >> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
> >> index 14db880..1235b37 100644
> >> --- a/libavformat/movenc.c
> >> +++ b/libavformat/movenc.c
> >> @@ -4919,6 +4919,18 @@ static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
> >>                      trk->start_cts = pkt->pts - pkt->dts;
> >>                  else
> >>                      trk->start_cts = 0;
> >> +            }
> >> +
> >> +            if (trk->par->codec_id == AV_CODEC_ID_MP4ALS) {
> >> +                int side_size = 0;
> >> +                uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
> >> +                if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
> >> +                    av_free(par->extradata);
> >
> >> +                    par->extradata = av_mallocz(side_size + AV_INPUT_BUFFER_PADDING_SIZE);
> >> +                    memcpy(par->extradata, side, side_size);
> >
> > missing check for malloc failure
> 
> Updated patch attached.
> 
> - Umair

>  movenc.c |   31 +++++++++++++++++++++++++++++++
>  movenc.h |    2 ++
>  2 files changed, 33 insertions(+)
> 9a062ecc1177d22c09112776a9079c86e9c71cb3  patch.diff
> From 57cf341487672f60e0304ebc863f6cae5e55e676 Mon Sep 17 00:00:00 2001
> From: Umair Khan <omerjerk@gmail.com>
> Date: Thu, 18 Aug 2016 00:27:42 +0530
> Subject: [PATCH] avformat/movenc: allow rewriting extradata
> 
> Signed-off-by: Umair Khan <omerjerk@gmail.com>
> ---
>  libavformat/movenc.c | 31 +++++++++++++++++++++++++++++++
>  libavformat/movenc.h |  2 ++
>  2 files changed, 33 insertions(+)

applied

ill change this slightly to be more robust in the error case (it
doesnt clear extradata_size i think if extradata is freed in the error
case

thx

[...]
diff mbox

Patch

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 14db880..137f437 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4919,6 +4919,21 @@  static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
                     trk->start_cts = pkt->pts - pkt->dts;
                 else
                     trk->start_cts = 0;
+            }       
+
+            if (trk->par->codec_id == AV_CODEC_ID_MP4ALS) {
+                int side_size = 0;
+                uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
+                if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
+                    av_free(par->extradata);
+                    par->extradata = av_mallocz(side_size + AV_INPUT_BUFFER_PADDING_SIZE);
+                    if (!par->extradata) {
+                        return AVERROR(ENOMEM);
+                    }
+                    memcpy(par->extradata, side, side_size);
+                    par->extradata_size = side_size;
+                    mov->need_rewrite_extradata = 1;
+                }
             }
 
             return 0;             /* Discard 0 sized packets */
@@ -5923,6 +5938,22 @@  static int mov_write_trailer(AVFormatContext *s)
     int i;
     int64_t moov_pos;
 
+    if (mov->need_rewrite_extradata) {
+        for (i = 0; i < s->nb_streams; i++) {
+            MOVTrack *track = &mov->tracks[i];
+            AVCodecParameters *par = track->par;
+
+            track->vos_len  = par->extradata_size;
+            track->vos_data = av_malloc(track->vos_len);
+            if (!track->vos_data) {
+                AVERROR(ENOMEM);
+                goto error;
+            }
+            memcpy(track->vos_data, par->extradata, track->vos_len);
+        }
+        mov->need_rewrite_extradata = 0;
+    }
+
     /*
      * Before actually writing the trailer, make sure that there are no
      * dangling subtitles, that need a terminating sample.
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 6e9f5ac..894a1b0 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -215,6 +215,8 @@  typedef struct MOVMuxContext {
     uint8_t *encryption_kid;
     int encryption_kid_len;
 
+    int need_rewrite_extradata;
+
 } MOVMuxContext;
 
 #define FF_MOV_FLAG_RTP_HINT              (1 <<  0)