diff mbox series

[FFmpeg-devel,2/3] avdevice/opengl: change codec to wrapped avframe

Message ID 20200405233824.29682-2-cus@passwd.hu
State New
Headers show
Series [FFmpeg-devel,1/3] avdevice/xv: change codec to wrapped avframe
Related show

Checks

Context Check Description
andriy/ffmpeg-patchwork pending
andriy/ffmpeg-patchwork success Applied patch
andriy/ffmpeg-patchwork success Configure finished
andriy/ffmpeg-patchwork success Make finished
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Marton Balint April 5, 2020, 11:38 p.m. UTC
Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavdevice/opengl_enc.c | 54 +++++++++---------------------------------------
 1 file changed, 10 insertions(+), 44 deletions(-)

Comments

Nicolas George April 6, 2020, 12:34 p.m. UTC | #1
Marton Balint (12020-04-06):
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  libavdevice/opengl_enc.c | 54 +++++++++---------------------------------------
>  1 file changed, 10 insertions(+), 44 deletions(-)

Same remark as for xv: adding support for the more efficient API is
useful; removing support for rawvideo is an API break.

> 
> diff --git a/libavdevice/opengl_enc.c b/libavdevice/opengl_enc.c
> index ae03caa8c5..b8bc46ebb5 100644
> --- a/libavdevice/opengl_enc.c
> +++ b/libavdevice/opengl_enc.c
> @@ -277,7 +277,7 @@ static const struct OpenGLFormatDesc {
>  };
>  
>  static av_cold int opengl_prepare_vertex(AVFormatContext *s);
> -static int opengl_draw(AVFormatContext *h, void *intput, int repaint, int is_pkt);
> +static int opengl_draw(AVFormatContext *h, void *intput, int repaint);
>  static av_cold int opengl_init_context(OpenGLContext *opengl);
>  
>  static av_cold void opengl_deinit_context(OpenGLContext *opengl)
> @@ -320,7 +320,7 @@ static int opengl_resize(AVFormatContext *h, int width, int height)
>          }
>          if ((ret = opengl_prepare_vertex(h)) < 0)
>              goto end;
> -        ret = opengl_draw(h, NULL, 1, 0);
> +        ret = opengl_draw(h, NULL, 1);
>      }
>    end:
>      return ret;
> @@ -1060,8 +1060,8 @@ static av_cold int opengl_write_header(AVFormatContext *h)
>  
>      if (h->nb_streams != 1 ||
>          h->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
> -        h->streams[0]->codecpar->codec_id != AV_CODEC_ID_RAWVIDEO) {
> -        av_log(opengl, AV_LOG_ERROR, "Only a single video stream is supported.\n");
> +        h->streams[0]->codecpar->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME) {
> +        av_log(opengl, AV_LOG_ERROR, "Only a single wrapped avframe stream is supported.\n");
>          return AVERROR(EINVAL);
>      }
>      st = h->streams[0];
> @@ -1124,35 +1124,6 @@ static av_cold int opengl_write_header(AVFormatContext *h)
>      return ret;
>  }
>  

> -static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, int comp_index,
> -                                         const AVPixFmtDescriptor *desc)
> -{
> -    uint8_t *data = pkt->data;
> -    int wordsize = opengl_type_size(opengl->type);
> -    int width_chroma = AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w);
> -    int height_chroma = AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h);
> -    int plane = desc->comp[comp_index].plane;
> -
> -    switch(plane) {
> -    case 0:
> -        break;
> -    case 1:
> -        data += opengl->width * opengl->height * wordsize;
> -        break;
> -    case 2:
> -        data += opengl->width * opengl->height * wordsize;
> -        data += width_chroma * height_chroma * wordsize;
> -        break;
> -    case 3:
> -        data += opengl->width * opengl->height * wordsize;
> -        data += 2 * width_chroma * height_chroma * wordsize;
> -        break;
> -    default:
> -        return NULL;
> -    }
> -    return data;
> -}

This could probably be replaced with av_image_fill_pointers(). But it is
unrelated.

> -
>  #define LOAD_TEXTURE_DATA(comp_index, sub)                                                  \
>  {                                                                                           \
>      int width = sub ? AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w) : opengl->width;   \
> @@ -1161,7 +1132,7 @@ static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, i
>      int plane = desc->comp[comp_index].plane;                                               \
>                                                                                              \
>      glBindTexture(GL_TEXTURE_2D, opengl->texture_name[comp_index]);                         \
> -    if (!is_pkt) {                                                                          \
> +    {                                                                                       \
>          GLint length = ((AVFrame *)input)->linesize[plane];                                 \
>          int bytes_per_pixel = opengl_type_size(opengl->type);                               \
>          if (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR))                                        \
> @@ -1184,14 +1155,10 @@ static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, i
>                  data += length;                                                             \
>              }                                                                               \
>          }                                                                                   \
> -    } else {                                                                                \
> -        data = opengl_get_plane_pointer(opengl, input, comp_index, desc);                   \
> -        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,                              \
> -                        opengl->format, opengl->type, data);                                \
>      }                                                                                       \
>  }
>  
> -static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
> +static int opengl_draw(AVFormatContext *h, void *input, int repaint)
>  {
>      OpenGLContext *opengl = h->priv_data;
>      enum AVPixelFormat pix_fmt = h->streams[0]->codecpar->format;
> @@ -1211,8 +1178,6 @@ static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
>      glClear(GL_COLOR_BUFFER_BIT);
>  
>      if (!repaint) {
> -        if (is_pkt)
> -            glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
>          LOAD_TEXTURE_DATA(0, 0)
>          if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) {
>              LOAD_TEXTURE_DATA(1, 1)
> @@ -1256,7 +1221,8 @@ static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
>  
>  static int opengl_write_packet(AVFormatContext *h, AVPacket *pkt)
>  {
> -    return opengl_draw(h, pkt, 0, 1);
> +    AVFrame *frame = (AVFrame *)pkt->data;
> +    return opengl_draw(h, frame, 0);
>  }
>  
>  static int opengl_write_frame(AVFormatContext *h, int stream_index,
> @@ -1264,7 +1230,7 @@ static int opengl_write_frame(AVFormatContext *h, int stream_index,
>  {
>      if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
>          return 0;
> -    return opengl_draw(h, *frame, 0, 0);
> +    return opengl_draw(h, *frame, 0);
>  }
>  
>  #define OFFSET(x) offsetof(OpenGLContext, x)
> @@ -1290,7 +1256,7 @@ AVOutputFormat ff_opengl_muxer = {
>      .long_name      = NULL_IF_CONFIG_SMALL("OpenGL output"),
>      .priv_data_size = sizeof(OpenGLContext),
>      .audio_codec    = AV_CODEC_ID_NONE,
> -    .video_codec    = AV_CODEC_ID_RAWVIDEO,
> +    .video_codec    = AV_CODEC_ID_WRAPPED_AVFRAME,
>      .write_header   = opengl_write_header,
>      .write_packet   = opengl_write_packet,
>      .write_uncoded_frame = opengl_write_frame,

Regards,
diff mbox series

Patch

diff --git a/libavdevice/opengl_enc.c b/libavdevice/opengl_enc.c
index ae03caa8c5..b8bc46ebb5 100644
--- a/libavdevice/opengl_enc.c
+++ b/libavdevice/opengl_enc.c
@@ -277,7 +277,7 @@  static const struct OpenGLFormatDesc {
 };
 
 static av_cold int opengl_prepare_vertex(AVFormatContext *s);
-static int opengl_draw(AVFormatContext *h, void *intput, int repaint, int is_pkt);
+static int opengl_draw(AVFormatContext *h, void *intput, int repaint);
 static av_cold int opengl_init_context(OpenGLContext *opengl);
 
 static av_cold void opengl_deinit_context(OpenGLContext *opengl)
@@ -320,7 +320,7 @@  static int opengl_resize(AVFormatContext *h, int width, int height)
         }
         if ((ret = opengl_prepare_vertex(h)) < 0)
             goto end;
-        ret = opengl_draw(h, NULL, 1, 0);
+        ret = opengl_draw(h, NULL, 1);
     }
   end:
     return ret;
@@ -1060,8 +1060,8 @@  static av_cold int opengl_write_header(AVFormatContext *h)
 
     if (h->nb_streams != 1 ||
         h->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ||
-        h->streams[0]->codecpar->codec_id != AV_CODEC_ID_RAWVIDEO) {
-        av_log(opengl, AV_LOG_ERROR, "Only a single video stream is supported.\n");
+        h->streams[0]->codecpar->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME) {
+        av_log(opengl, AV_LOG_ERROR, "Only a single wrapped avframe stream is supported.\n");
         return AVERROR(EINVAL);
     }
     st = h->streams[0];
@@ -1124,35 +1124,6 @@  static av_cold int opengl_write_header(AVFormatContext *h)
     return ret;
 }
 
-static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, int comp_index,
-                                         const AVPixFmtDescriptor *desc)
-{
-    uint8_t *data = pkt->data;
-    int wordsize = opengl_type_size(opengl->type);
-    int width_chroma = AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w);
-    int height_chroma = AV_CEIL_RSHIFT(opengl->height, desc->log2_chroma_h);
-    int plane = desc->comp[comp_index].plane;
-
-    switch(plane) {
-    case 0:
-        break;
-    case 1:
-        data += opengl->width * opengl->height * wordsize;
-        break;
-    case 2:
-        data += opengl->width * opengl->height * wordsize;
-        data += width_chroma * height_chroma * wordsize;
-        break;
-    case 3:
-        data += opengl->width * opengl->height * wordsize;
-        data += 2 * width_chroma * height_chroma * wordsize;
-        break;
-    default:
-        return NULL;
-    }
-    return data;
-}
-
 #define LOAD_TEXTURE_DATA(comp_index, sub)                                                  \
 {                                                                                           \
     int width = sub ? AV_CEIL_RSHIFT(opengl->width, desc->log2_chroma_w) : opengl->width;   \
@@ -1161,7 +1132,7 @@  static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, i
     int plane = desc->comp[comp_index].plane;                                               \
                                                                                             \
     glBindTexture(GL_TEXTURE_2D, opengl->texture_name[comp_index]);                         \
-    if (!is_pkt) {                                                                          \
+    {                                                                                       \
         GLint length = ((AVFrame *)input)->linesize[plane];                                 \
         int bytes_per_pixel = opengl_type_size(opengl->type);                               \
         if (!(desc->flags & AV_PIX_FMT_FLAG_PLANAR))                                        \
@@ -1184,14 +1155,10 @@  static uint8_t* opengl_get_plane_pointer(OpenGLContext *opengl, AVPacket *pkt, i
                 data += length;                                                             \
             }                                                                               \
         }                                                                                   \
-    } else {                                                                                \
-        data = opengl_get_plane_pointer(opengl, input, comp_index, desc);                   \
-        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,                              \
-                        opengl->format, opengl->type, data);                                \
     }                                                                                       \
 }
 
-static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
+static int opengl_draw(AVFormatContext *h, void *input, int repaint)
 {
     OpenGLContext *opengl = h->priv_data;
     enum AVPixelFormat pix_fmt = h->streams[0]->codecpar->format;
@@ -1211,8 +1178,6 @@  static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
     glClear(GL_COLOR_BUFFER_BIT);
 
     if (!repaint) {
-        if (is_pkt)
-            glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
         LOAD_TEXTURE_DATA(0, 0)
         if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) {
             LOAD_TEXTURE_DATA(1, 1)
@@ -1256,7 +1221,8 @@  static int opengl_draw(AVFormatContext *h, void *input, int repaint, int is_pkt)
 
 static int opengl_write_packet(AVFormatContext *h, AVPacket *pkt)
 {
-    return opengl_draw(h, pkt, 0, 1);
+    AVFrame *frame = (AVFrame *)pkt->data;
+    return opengl_draw(h, frame, 0);
 }
 
 static int opengl_write_frame(AVFormatContext *h, int stream_index,
@@ -1264,7 +1230,7 @@  static int opengl_write_frame(AVFormatContext *h, int stream_index,
 {
     if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
         return 0;
-    return opengl_draw(h, *frame, 0, 0);
+    return opengl_draw(h, *frame, 0);
 }
 
 #define OFFSET(x) offsetof(OpenGLContext, x)
@@ -1290,7 +1256,7 @@  AVOutputFormat ff_opengl_muxer = {
     .long_name      = NULL_IF_CONFIG_SMALL("OpenGL output"),
     .priv_data_size = sizeof(OpenGLContext),
     .audio_codec    = AV_CODEC_ID_NONE,
-    .video_codec    = AV_CODEC_ID_RAWVIDEO,
+    .video_codec    = AV_CODEC_ID_WRAPPED_AVFRAME,
     .write_header   = opengl_write_header,
     .write_packet   = opengl_write_packet,
     .write_uncoded_frame = opengl_write_frame,