diff mbox

[FFmpeg-devel,1/4] avformat/movenc: add support for writing Mastering Display Metadata Box

Message ID 20170518004941.5140-1-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer May 18, 2017, 12:49 a.m. UTC
As defined in "VP Codec ISO Media File Format Binding v1.0"
https://github.com/webmproject/vp9-dash/blob/master/VPCodecISOMediaFileFormatBinding.md

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavformat/movenc.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

Comments

James Almer May 24, 2017, 3:53 a.m. UTC | #1
On 5/17/2017 9:49 PM, James Almer wrote:
> As defined in "VP Codec ISO Media File Format Binding v1.0"
> https://github.com/webmproject/vp9-dash/blob/master/VPCodecISOMediaFileFormatBinding.md
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavformat/movenc.c | 37 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 37 insertions(+)

Ping for set.
Michael Niedermayer May 26, 2017, 10:48 p.m. UTC | #2
On Wed, May 17, 2017 at 09:49:38PM -0300, James Almer wrote:
> As defined in "VP Codec ISO Media File Format Binding v1.0"
> https://github.com/webmproject/vp9-dash/blob/master/VPCodecISOMediaFileFormatBinding.md
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavformat/movenc.c | 37 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 37 insertions(+)
> 
> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
> index a6c0662cd0..cd436df7a4 100644
> --- a/libavformat/movenc.c
> +++ b/libavformat/movenc.c
> @@ -42,6 +42,7 @@
>  #include "libavutil/avstring.h"
>  #include "libavutil/intfloat.h"
>  #include "libavutil/mathematics.h"
> +#include "libavutil/mastering_display_metadata.h"
>  #include "libavutil/libm.h"
>  #include "libavutil/opt.h"
>  #include "libavutil/dict.h"
> @@ -1118,6 +1119,41 @@ static int mov_write_vpcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
>      return update_size(pb, pos);
>  }
>  
> +static int mov_write_smdm_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
> +{
> +    int size = 0;
> +    int64_t pos;
> +    const AVMasteringDisplayMetadata *mastering =
> +        (const AVMasteringDisplayMetadata *) av_stream_get_side_data(track->st,
> +                                                 AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
> +                                                 &size);
> +    if (!size)
> +        return 0;
> +
> +    if (!mastering->has_primaries || !mastering->has_luminance) {
> +        av_log(s, AV_LOG_WARNING, "Incomplete Mastering Display metadata. Both luminance "
> +                                  "and display primaries are needed\n");
> +        return 0;
> +    }
> +
> +    pos = avio_tell(pb);
> +
> +    avio_wb32(pb, 0);
> +    ffio_wfourcc(pb, "SmDm");
> +    avio_wb32(pb, 0); /* version & flags */

> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[0][0]) * (1 << 16)));
> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[0][1]) * (1 << 16)));
> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[1][0]) * (1 << 16)));
> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[1][1]) * (1 << 16)));
> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[2][0]) * (1 << 16)));
> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[2][1]) * (1 << 16)));
> +    avio_wb16(pb, lrint(av_q2d(mastering->white_point[0])          * (1 << 16)));
> +    avio_wb16(pb, lrint(av_q2d(mastering->white_point[1])          * (1 << 16)));
> +    avio_wb32(pb, lrint(av_q2d(mastering->max_luminance)           * (1 <<  8)));
> +    avio_wb32(pb, lrint(av_q2d(mastering->min_luminance)           * (1 << 14)));

These may need range checks.
Our API doesnt seem to define limits, so they might fall outside the
16/32 bit range used to store them, unless i miss something

[...]
James Almer May 27, 2017, 12:14 a.m. UTC | #3
On 5/26/2017 7:48 PM, Michael Niedermayer wrote:
> On Wed, May 17, 2017 at 09:49:38PM -0300, James Almer wrote:
>> As defined in "VP Codec ISO Media File Format Binding v1.0"
>> https://github.com/webmproject/vp9-dash/blob/master/VPCodecISOMediaFileFormatBinding.md
>>
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>  libavformat/movenc.c | 37 +++++++++++++++++++++++++++++++++++++
>>  1 file changed, 37 insertions(+)
>>
>> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
>> index a6c0662cd0..cd436df7a4 100644
>> --- a/libavformat/movenc.c
>> +++ b/libavformat/movenc.c
>> @@ -42,6 +42,7 @@
>>  #include "libavutil/avstring.h"
>>  #include "libavutil/intfloat.h"
>>  #include "libavutil/mathematics.h"
>> +#include "libavutil/mastering_display_metadata.h"
>>  #include "libavutil/libm.h"
>>  #include "libavutil/opt.h"
>>  #include "libavutil/dict.h"
>> @@ -1118,6 +1119,41 @@ static int mov_write_vpcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
>>      return update_size(pb, pos);
>>  }
>>  
>> +static int mov_write_smdm_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
>> +{
>> +    int size = 0;
>> +    int64_t pos;
>> +    const AVMasteringDisplayMetadata *mastering =
>> +        (const AVMasteringDisplayMetadata *) av_stream_get_side_data(track->st,
>> +                                                 AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
>> +                                                 &size);
>> +    if (!size)
>> +        return 0;
>> +
>> +    if (!mastering->has_primaries || !mastering->has_luminance) {
>> +        av_log(s, AV_LOG_WARNING, "Incomplete Mastering Display metadata. Both luminance "
>> +                                  "and display primaries are needed\n");
>> +        return 0;
>> +    }
>> +
>> +    pos = avio_tell(pb);
>> +
>> +    avio_wb32(pb, 0);
>> +    ffio_wfourcc(pb, "SmDm");
>> +    avio_wb32(pb, 0); /* version & flags */
> 
>> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[0][0]) * (1 << 16)));
>> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[0][1]) * (1 << 16)));
>> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[1][0]) * (1 << 16)));
>> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[1][1]) * (1 << 16)));
>> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[2][0]) * (1 << 16)));
>> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[2][1]) * (1 << 16)));
>> +    avio_wb16(pb, lrint(av_q2d(mastering->white_point[0])          * (1 << 16)));
>> +    avio_wb16(pb, lrint(av_q2d(mastering->white_point[1])          * (1 << 16)));
>> +    avio_wb32(pb, lrint(av_q2d(mastering->max_luminance)           * (1 <<  8)));
>> +    avio_wb32(pb, lrint(av_q2d(mastering->min_luminance)           * (1 << 14)));
> 
> These may need range checks.
> Our API doesnt seem to define limits, so they might fall outside the
> 16/32 bit range used to store them, unless i miss something

I guess. How do you suggest to check this? Range checking the
AVRationals with av_cmp_q()?

This could have been avoided if mastering metadata fields were
implemented as integer values, like content light does.
Michael Niedermayer May 27, 2017, 2:10 p.m. UTC | #4
On Fri, May 26, 2017 at 09:14:19PM -0300, James Almer wrote:
> On 5/26/2017 7:48 PM, Michael Niedermayer wrote:
> > On Wed, May 17, 2017 at 09:49:38PM -0300, James Almer wrote:
> >> As defined in "VP Codec ISO Media File Format Binding v1.0"
> >> https://github.com/webmproject/vp9-dash/blob/master/VPCodecISOMediaFileFormatBinding.md
> >>
> >> Signed-off-by: James Almer <jamrial@gmail.com>
> >> ---
> >>  libavformat/movenc.c | 37 +++++++++++++++++++++++++++++++++++++
> >>  1 file changed, 37 insertions(+)
> >>
> >> diff --git a/libavformat/movenc.c b/libavformat/movenc.c
> >> index a6c0662cd0..cd436df7a4 100644
> >> --- a/libavformat/movenc.c
> >> +++ b/libavformat/movenc.c
> >> @@ -42,6 +42,7 @@
> >>  #include "libavutil/avstring.h"
> >>  #include "libavutil/intfloat.h"
> >>  #include "libavutil/mathematics.h"
> >> +#include "libavutil/mastering_display_metadata.h"
> >>  #include "libavutil/libm.h"
> >>  #include "libavutil/opt.h"
> >>  #include "libavutil/dict.h"
> >> @@ -1118,6 +1119,41 @@ static int mov_write_vpcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
> >>      return update_size(pb, pos);
> >>  }
> >>  
> >> +static int mov_write_smdm_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
> >> +{
> >> +    int size = 0;
> >> +    int64_t pos;
> >> +    const AVMasteringDisplayMetadata *mastering =
> >> +        (const AVMasteringDisplayMetadata *) av_stream_get_side_data(track->st,
> >> +                                                 AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
> >> +                                                 &size);
> >> +    if (!size)
> >> +        return 0;
> >> +
> >> +    if (!mastering->has_primaries || !mastering->has_luminance) {
> >> +        av_log(s, AV_LOG_WARNING, "Incomplete Mastering Display metadata. Both luminance "
> >> +                                  "and display primaries are needed\n");
> >> +        return 0;
> >> +    }
> >> +
> >> +    pos = avio_tell(pb);
> >> +
> >> +    avio_wb32(pb, 0);
> >> +    ffio_wfourcc(pb, "SmDm");
> >> +    avio_wb32(pb, 0); /* version & flags */
> > 
> >> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[0][0]) * (1 << 16)));
> >> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[0][1]) * (1 << 16)));
> >> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[1][0]) * (1 << 16)));
> >> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[1][1]) * (1 << 16)));
> >> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[2][0]) * (1 << 16)));
> >> +    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[2][1]) * (1 << 16)));
> >> +    avio_wb16(pb, lrint(av_q2d(mastering->white_point[0])          * (1 << 16)));
> >> +    avio_wb16(pb, lrint(av_q2d(mastering->white_point[1])          * (1 << 16)));
> >> +    avio_wb32(pb, lrint(av_q2d(mastering->max_luminance)           * (1 <<  8)));
> >> +    avio_wb32(pb, lrint(av_q2d(mastering->min_luminance)           * (1 << 14)));
> > 
> > These may need range checks.
> > Our API doesnt seem to define limits, so they might fall outside the
> > 16/32 bit range used to store them, unless i miss something
> 
> I guess. How do you suggest to check this? Range checking the
> AVRationals with av_cmp_q()?

If i would write the code, i probably would use a temporare variable
and check that before storing with avio_wbXY()
strictly one needs long long for that probably and iam not sure the
denominator needs to be checked for 0
maybe theres some simpler way though, i dont know


> 
> This could have been avoided if mastering metadata fields were
> implemented as integer values, like content light does.
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
diff mbox

Patch

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index a6c0662cd0..cd436df7a4 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -42,6 +42,7 @@ 
 #include "libavutil/avstring.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mastering_display_metadata.h"
 #include "libavutil/libm.h"
 #include "libavutil/opt.h"
 #include "libavutil/dict.h"
@@ -1118,6 +1119,41 @@  static int mov_write_vpcc_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
     return update_size(pb, pos);
 }
 
+static int mov_write_smdm_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track)
+{
+    int size = 0;
+    int64_t pos;
+    const AVMasteringDisplayMetadata *mastering =
+        (const AVMasteringDisplayMetadata *) av_stream_get_side_data(track->st,
+                                                 AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
+                                                 &size);
+    if (!size)
+        return 0;
+
+    if (!mastering->has_primaries || !mastering->has_luminance) {
+        av_log(s, AV_LOG_WARNING, "Incomplete Mastering Display metadata. Both luminance "
+                                  "and display primaries are needed\n");
+        return 0;
+    }
+
+    pos = avio_tell(pb);
+
+    avio_wb32(pb, 0);
+    ffio_wfourcc(pb, "SmDm");
+    avio_wb32(pb, 0); /* version & flags */
+    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[0][0]) * (1 << 16)));
+    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[0][1]) * (1 << 16)));
+    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[1][0]) * (1 << 16)));
+    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[1][1]) * (1 << 16)));
+    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[2][0]) * (1 << 16)));
+    avio_wb16(pb, lrint(av_q2d(mastering->display_primaries[2][1]) * (1 << 16)));
+    avio_wb16(pb, lrint(av_q2d(mastering->white_point[0])          * (1 << 16)));
+    avio_wb16(pb, lrint(av_q2d(mastering->white_point[1])          * (1 << 16)));
+    avio_wb32(pb, lrint(av_q2d(mastering->max_luminance)           * (1 <<  8)));
+    avio_wb32(pb, lrint(av_q2d(mastering->min_luminance)           * (1 << 14)));
+    return update_size(pb, pos);
+}
+
 static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
 {
     int64_t pos = avio_tell(pb);
@@ -1964,6 +2000,7 @@  static int mov_write_video_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *tr
             mov_write_uuid_tag_ipod(pb);
     } else if (track->par->codec_id == AV_CODEC_ID_VP9) {
         mov_write_vpcc_tag(mov->fc, pb, track);
+        mov_write_smdm_tag(mov->fc, pb, track);
     } else if (track->par->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
         mov_write_dvc1_tag(pb, track);
     else if (track->par->codec_id == AV_CODEC_ID_VP6F ||