[FFmpeg-devel] [PATCH v2] avformat/matroskaenc: add support for Spherical Video elements

James Almer jamrial at gmail.com
Thu Mar 9 19:11:40 EET 2017


On 3/8/2017 6:24 PM, James Almer wrote:
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  libavformat/matroskaenc.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 75 insertions(+)
> 
> diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
> index 1605f0cafe..70b0ced226 100644
> --- a/libavformat/matroskaenc.c
> +++ b/libavformat/matroskaenc.c
> @@ -918,6 +918,78 @@ static int mkv_write_video_color(AVIOContext *pb, AVCodecParameters *par, AVStre
>      return 0;
>  }
>  
> +static int mkv_write_video_projection(AVIOContext *pb, AVStream *st) {
> +    int side_data_size = 0;
> +    const AVSphericalMapping *spherical =
> +        (const AVSphericalMapping*) av_stream_get_side_data(st, AV_PKT_DATA_SPHERICAL,
> +                                                            &side_data_size);
> +
> +    if (side_data_size) {
> +        AVIOContext *dyn_cp;
> +        uint8_t *projection_ptr;
> +        int ret, projection_size;
> +
> +        ret = avio_open_dyn_buf(&dyn_cp);
> +        if (ret < 0)
> +            return ret;
> +
> +        switch (spherical->projection) {
> +        case AV_SPHERICAL_EQUIRECTANGULAR_TILE:
> +        {
> +            uint8_t private[20];
> +            AVIOContext b;
> +            ffio_init_context(&b, private, sizeof(private),
> +                              1, NULL, NULL, NULL, NULL);
> +            put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONTYPE,
> +                          MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR);
> +            avio_wb32(&b, 0); // version + flags
> +            avio_wb32(&b, spherical->bound_top);
> +            avio_wb32(&b, spherical->bound_bottom);
> +            avio_wb32(&b, spherical->bound_left);
> +            avio_wb32(&b, spherical->bound_right);
> +            put_ebml_binary(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPRIVATE, private, sizeof(private));
> +            break;
> +        }
> +        case AV_SPHERICAL_EQUIRECTANGULAR:
> +            put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONTYPE,
> +                          MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR);
> +            break;
> +        case AV_SPHERICAL_CUBEMAP:
> +        {
> +            uint8_t private[12];
> +            AVIOContext b;
> +            ffio_init_context(&b, private, sizeof(private),
> +                              1, NULL, NULL, NULL, NULL);
> +            put_ebml_uint(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONTYPE,
> +                          MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP);
> +            avio_wb32(&b, 0); // version + flags
> +            avio_wb32(&b, 0); // layout
> +            avio_wb32(&b, spherical->padding);
> +            put_ebml_binary(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPRIVATE, private, sizeof(private));
> +            break;
> +        }
> +        default:
> +            // TODO: Mesh projection once implemented in AVSphericalMapping
> +            goto end;
> +        }
> +
> +        put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPOSEYAW,   (double)spherical->yaw   / (1 << 16));
> +        put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH, (double)spherical->pitch / (1 << 16));
> +        put_ebml_float(dyn_cp, MATROSKA_ID_VIDEOPROJECTIONPOSEROLL,  (double)spherical->roll  / (1 << 16));
> +
> +end:
> +        projection_size = avio_close_dyn_buf(dyn_cp, &projection_ptr);
> +        if (projection_size) {
> +            ebml_master projection = start_ebml_master(pb, MATROSKA_ID_VIDEOPROJECTION, projection_size);
> +            avio_write(pb, projection_ptr, projection_size);
> +            end_ebml_master(pb, projection);
> +        }
> +        av_freep(&projection_ptr);
> +    }
> +
> +    return 0;
> +}
> +
>  static void mkv_write_field_order(AVIOContext *pb, int mode,
>                                    enum AVFieldOrder field_order)
>  {
> @@ -1268,6 +1340,9 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
>          ret = mkv_write_video_color(pb, par, st);
>          if (ret < 0)
>              return ret;
> +        ret = mkv_write_video_projection(pb, st);
> +        if (ret < 0)
> +            return ret;
>          end_ebml_master(pb, subinfo);
>          break;

Added a log message on unknown projections as requested and pushed.



More information about the ffmpeg-devel mailing list