]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ansi.c
vda: remove async decoder leftovers
[ffmpeg] / libavcodec / ansi.c
index 6fa62cc680df4ad68efddeac22a2e83d29bf26a3..b99826b3a5cd20fc302425ed15d01bdfbb88ca95 100644 (file)
@@ -2,20 +2,20 @@
  * ASCII/ANSI art decoder
  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
  * ASCII/ANSI art decoder
  */
 
+#include "libavutil/common.h"
+#include "libavutil/frame.h"
+#include "libavutil/lfg.h"
 #include "avcodec.h"
 #include "cga_data.h"
-#include <libavutil/lfg.h>
+#include "internal.h"
 
-#define ATTR_BOLD         0x01  /** Bold/Bright-foreground (mode 1) */
-#define ATTR_FAINT        0x02  /** Faint (mode 2) */
-#define ATTR_UNDERLINE    0x08  /** Underline (mode 4) */
-#define ATTR_BLINK        0x10  /** Blink/Bright-background (mode 5) */
-#define ATTR_REVERSE      0x40  /** Reverse (mode 7) */
-#define ATTR_CONCEALED    0x80  /** Concealed (mode 8) */
+#define ATTR_BOLD         0x01  /**< Bold/Bright-foreground (mode 1) */
+#define ATTR_FAINT        0x02  /**< Faint (mode 2) */
+#define ATTR_UNDERLINE    0x08  /**< Underline (mode 4) */
+#define ATTR_BLINK        0x10  /**< Blink/Bright-background (mode 5) */
+#define ATTR_REVERSE      0x40  /**< Reverse (mode 7) */
+#define ATTR_CONCEALED    0x80  /**< Concealed (mode 8) */
 
-#define DEFAULT_FG_COLOR     7  /** CGA color index */
+#define DEFAULT_FG_COLOR     7  /**< CGA color index */
 #define DEFAULT_BG_COLOR     0
-#define DEFAULT_SCREEN_MODE  3  /** 80x25 */
+#define DEFAULT_SCREEN_MODE  3  /**< 80x25 */
 
-#define FONT_WIDTH           8  /** Font width */
+#define FONT_WIDTH           8  /**< Font width */
 
 /** map ansi color index to cga palette index */
 static const uint8_t ansi_to_cga[16] = {
@@ -47,13 +50,16 @@ static const uint8_t ansi_to_cga[16] = {
 };
 
 typedef struct {
-    AVFrame frame;
-    int x, y;             /** cursor position (pixels) */
-    int sx, sy;           /** saved cursor position (pixels) */
-    const uint8_t* font;  /** font */
-    int font_height;      /** font height */
-    int attributes;       /** attribute flags */
-    int fg, bg;           /** foreground and background color */
+    AVFrame *frame;
+    int x;                /**< x cursor position (pixels) */
+    int y;                /**< y cursor position (pixels) */
+    int sx;               /**< saved x cursor position (pixels) */
+    int sy;               /**< saved y cursor position (pixels) */
+    const uint8_t* font;  /**< font */
+    int font_height;      /**< font height */
+    int attributes;       /**< attribute flags */
+    int fg;               /**< foreground color */
+    int bg;               /**< background color */
 
     /* ansi parser state machine */
     enum {
@@ -64,13 +70,17 @@ typedef struct {
     } state;
 #define MAX_NB_ARGS 4
     int args[MAX_NB_ARGS];
-    int nb_args;          /** number of arguments (may exceed MAX_NB_ARGS) */
+    int nb_args;          /**< number of arguments (may exceed MAX_NB_ARGS) */
 } AnsiContext;
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     AnsiContext *s = avctx->priv_data;
-    avctx->pix_fmt = PIX_FMT_PAL8;
+    avctx->pix_fmt = AV_PIX_FMT_PAL8;
+
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
 
     /* defaults */
     s->font        = ff_vga16_font;
@@ -96,11 +106,11 @@ static void hscroll(AVCodecContext *avctx)
 
     i = 0;
     for (; i < avctx->height - s->font_height; i++)
-        memcpy(s->frame.data[0] + i * s->frame.linesize[0],
-               s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
+        memcpy(s->frame->data[0] + i * s->frame->linesize[0],
+               s->frame->data[0] + (i + s->font_height) * s->frame->linesize[0],
                avctx->width);
     for (; i < avctx->height; i++)
-        memset(s->frame.data[0] + i * s->frame.linesize[0],
+        memset(s->frame->data[0] + i * s->frame->linesize[0],
             DEFAULT_BG_COLOR, avctx->width);
 }
 
@@ -109,7 +119,7 @@ static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
     AnsiContext *s = avctx->priv_data;
     int i;
     for (i = 0; i < s->font_height; i++)
-        memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
+        memset(s->frame->data[0] + (s->y + i)*s->frame->linesize[0] + xoffset,
             DEFAULT_BG_COLOR, xlength);
 }
 
@@ -118,7 +128,7 @@ static void erase_screen(AVCodecContext *avctx)
     AnsiContext *s = avctx->priv_data;
     int i;
     for (i = 0; i < avctx->height; i++)
-        memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
+        memset(s->frame->data[0] + i * s->frame->linesize[0], DEFAULT_BG_COLOR, avctx->width);
     s->x = s->y = 0;
 }
 
@@ -139,8 +149,8 @@ static void draw_char(AVCodecContext *avctx, int c)
         FFSWAP(int, fg, bg);
     if ((s->attributes & ATTR_CONCEALED))
         fg = bg;
-    ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
-                    s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
+    ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
+                    s->frame->linesize[0], s->font, s->font_height, c, fg, bg);
     s->x += FONT_WIDTH;
     if (s->x >= avctx->width) {
         s->x = 0;
@@ -150,7 +160,7 @@ static void draw_char(AVCodecContext *avctx, int c)
 
 /**
  * Execute ANSI escape code
- * @param <0 error
+ * @return 0 on success, negative on error
  */
 static int execute_code(AVCodecContext * avctx, int c)
 {
@@ -215,17 +225,16 @@ static int execute_code(AVCodecContext * avctx, int c)
             av_log_ask_for_sample(avctx, "unsupported screen mode\n");
         }
         if (width != avctx->width || height != avctx->height) {
-            if (s->frame.data[0])
-                avctx->release_buffer(avctx, &s->frame);
+            av_frame_unref(s->frame);
             avcodec_set_dimensions(avctx, width, height);
-            ret = avctx->get_buffer(avctx, &s->frame);
+            ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF);
             if (ret < 0) {
                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
                 return ret;
             }
-            s->frame.pict_type           = FF_I_TYPE;
-            s->frame.palette_has_changed = 1;
-            memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
+            s->frame->pict_type           = AV_PICTURE_TYPE_I;
+            s->frame->palette_has_changed = 1;
+            memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);
             erase_screen(avctx);
         } else if (c == 'l') {
             erase_screen(avctx);
@@ -236,13 +245,13 @@ static int execute_code(AVCodecContext * avctx, int c)
         case 0:
             erase_line(avctx, s->x, avctx->width - s->x);
             if (s->y < avctx->height - s->font_height)
-                memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
-                    DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
+                memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],
+                    DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);
             break;
         case 1:
             erase_line(avctx, 0, s->x);
             if (s->y > 0)
-                memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
+                memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);
             break;
         case 2:
             erase_screen(avctx);
@@ -306,7 +315,7 @@ static int execute_code(AVCodecContext * avctx, int c)
 }
 
 static int decode_frame(AVCodecContext *avctx,
-                            void *data, int *data_size,
+                            void *data, int *got_frame,
                             AVPacket *avpkt)
 {
     AnsiContext *s = avctx->priv_data;
@@ -315,14 +324,19 @@ static int decode_frame(AVCodecContext *avctx,
     const uint8_t *buf_end   = buf+buf_size;
     int ret, i, count;
 
-    ret = avctx->reget_buffer(avctx, &s->frame);
+    ret = ff_reget_buffer(avctx, s->frame);
     if (ret < 0){
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return ret;
     }
-    s->frame.pict_type           = FF_I_TYPE;
-    s->frame.palette_has_changed = 1;
-    memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
+    if (!avctx->frame_number) {
+        memset(s->frame->data[0], 0, avctx->height * FFABS(s->frame->linesize[0]));
+        memset(s->frame->data[1], 0, AVPALETTE_SIZE);
+    }
+
+    s->frame->pict_type           = AV_PICTURE_TYPE_I;
+    s->frame->palette_has_changed = 1;
+    memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);
 
     while(buf < buf_end) {
         switch(s->state) {
@@ -365,7 +379,6 @@ static int decode_frame(AVCodecContext *avctx,
             } else {
                 s->state = STATE_NORMAL;
                 draw_char(avctx, 0x1B);
-                    return -1;
                 continue;
             }
             break;
@@ -392,8 +405,8 @@ static int decode_frame(AVCodecContext *avctx,
                     av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
                 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
                     s->nb_args++;
-                if (execute_code(avctx, buf[0]) < 0)
-                    return -1;
+                if ((ret = execute_code(avctx, buf[0])) < 0)
+                    return ret;
                 s->state = STATE_NORMAL;
             }
             break;
@@ -406,23 +419,24 @@ static int decode_frame(AVCodecContext *avctx,
         buf++;
     }
 
-    *data_size = sizeof(AVFrame);
-    *(AVFrame*)data = s->frame;
+    *got_frame = 1;
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
     return buf_size;
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
 {
     AnsiContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+
+    av_frame_free(&s->frame);
     return 0;
 }
 
-AVCodec ansi_decoder = {
+AVCodec ff_ansi_decoder = {
     .name           = "ansi",
-    .type           = CODEC_TYPE_VIDEO,
-    .id             = CODEC_ID_ANSI,
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_ANSI,
     .priv_data_size = sizeof(AnsiContext),
     .init           = decode_init,
     .close          = decode_close,