]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/a64multienc.c
dvbsubdec: check memory allocations and propagate errors
[ffmpeg] / libavcodec / a64multienc.c
index aed28ad280ced6f9e2c7c21654f4f06e54faec6a..d742deeaf70c3181ffdc982db49cee1506767335 100644 (file)
@@ -2,20 +2,20 @@
  * a64 video encoder - multicolor modes
  * Copyright (c) 2009 Tobias Bindhammer
  *
- * 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
  */
 
@@ -28,6 +28,8 @@
 #include "a64colors.h"
 #include "a64tables.h"
 #include "elbg.h"
+#include "internal.h"
+#include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
 
 #define DITHERSTEPS   8
@@ -163,6 +165,7 @@ static void render_charset(AVCodecContext *avctx, uint8_t *charset,
 static av_cold int a64multi_close_encoder(AVCodecContext *avctx)
 {
     A64Context *c = avctx->priv_data;
+    av_frame_free(&avctx->coded_frame);
     av_free(c->mc_meta_charset);
     av_free(c->mc_best_cb);
     av_free(c->mc_charset);
@@ -171,7 +174,7 @@ static av_cold int a64multi_close_encoder(AVCodecContext *avctx)
     return 0;
 }
 
-static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
+static av_cold int a64multi_encode_init(AVCodecContext *avctx)
 {
     A64Context *c = avctx->priv_data;
     int a;
@@ -186,7 +189,7 @@ static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
     av_log(avctx, AV_LOG_INFO, "charset lifetime set to %d frame(s)\n", c->mc_lifetime);
 
     c->mc_frame_counter = 0;
-    c->mc_use_5col      = avctx->codec->id == CODEC_ID_A64_MULTI5;
+    c->mc_use_5col      = avctx->codec->id == AV_CODEC_ID_A64_MULTI5;
     c->mc_pal_size      = 4 + c->mc_use_5col;
 
     /* precalc luma values for later use */
@@ -214,13 +217,19 @@ static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
     AV_WB32(avctx->extradata, c->mc_lifetime);
     AV_WB32(avctx->extradata + 16, INTERLACED);
 
-    avcodec_get_frame_defaults(&c->picture);
-    avctx->coded_frame            = &c->picture;
-    avctx->coded_frame->pict_type = FF_I_TYPE;
+    avctx->coded_frame = av_frame_alloc();
+    if (!avctx->coded_frame) {
+        a64multi_close_encoder(avctx);
+        return AVERROR(ENOMEM);
+    }
+
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
     avctx->coded_frame->key_frame = 1;
     if (!avctx->codec_tag)
          avctx->codec_tag = AV_RL32("a64m");
 
+    c->next_pts = AV_NOPTS_VALUE;
+
     return 0;
 }
 
@@ -239,20 +248,19 @@ static void a64_compress_colram(unsigned char *buf, int *charmap, uint8_t *colra
     }
 }
 
-static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
-                                 int buf_size, void *data)
+static int a64multi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
+                                 const AVFrame *pict, int *got_packet)
 {
     A64Context *c = avctx->priv_data;
-    AVFrame *pict = data;
-    AVFrame *const p = (AVFrame *) & c->picture;
+    AVFrame *const p = avctx->coded_frame;
 
     int frame;
     int x, y;
     int b_height;
     int b_width;
 
-    int req_size;
-    int num_frames   = c->mc_lifetime;
+    int req_size, ret;
+    uint8_t *buf;
 
     int *charmap     = c->mc_charmap;
     uint8_t *colram  = c->mc_colram;
@@ -275,12 +283,11 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
     }
 
     /* no data, means end encoding asap */
-    if (!data) {
+    if (!pict) {
         /* all done, end encoding */
         if (!c->mc_lifetime) return 0;
         /* no more frames in queue, prepare to flush remaining frames */
         if (!c->mc_frame_counter) {
-            num_frames = c->mc_lifetime;
             c->mc_lifetime = 0;
         }
         /* still frames in queue so limit lifetime to remaining frames */
@@ -290,10 +297,12 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
         /* fill up mc_meta_charset with data until lifetime exceeds */
         if (c->mc_frame_counter < c->mc_lifetime) {
             *p = *pict;
-            p->pict_type = FF_I_TYPE;
+            p->pict_type = AV_PICTURE_TYPE_I;
             p->key_frame = 1;
             to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
             c->mc_frame_counter++;
+            if (c->next_pts == AV_NOPTS_VALUE)
+                c->next_pts = pict->pts;
             /* lifetime is not reached so wait for next frame first */
             return 0;
         }
@@ -304,23 +313,33 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
         req_size = 0;
         /* any frames to encode? */
         if (c->mc_lifetime) {
+            req_size = charset_size + c->mc_lifetime*(screen_size + colram_size);
+            if ((ret = ff_alloc_packet(pkt, req_size)) < 0) {
+                av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", req_size);
+                return ret;
+            }
+            buf = pkt->data;
+
             /* calc optimal new charset + charmaps */
-            ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
-            ff_do_elbg  (meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
+            ret = ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb,
+                               CHARSET_CHARS, 50, charmap, &c->randctx);
+            if (ret < 0)
+                return ret;
+            ret = ff_do_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb,
+                             CHARSET_CHARS, 50, charmap, &c->randctx);
+            if (ret < 0)
+                return ret;
 
             /* create colorram map and a c64 readable charset */
             render_charset(avctx, charset, colram);
 
             /* copy charset to buf */
-            memcpy(buf,charset, charset_size);
+            memcpy(buf, charset, charset_size);
 
             /* advance pointers */
             buf      += charset_size;
             charset  += charset_size;
-            req_size += charset_size;
         }
-        /* no charset so clean buf */
-        else memset(buf, 0, charset_size);
 
         /* write x frames to buf */
         for (frame = 0; frame < c->mc_lifetime; frame++) {
@@ -353,37 +372,38 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
         /* reset counter */
         c->mc_frame_counter = 0;
 
-        if (req_size > buf_size) {
-            av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
-            return -1;
-        }
-        return req_size;
+        pkt->pts = pkt->dts = c->next_pts;
+        c->next_pts         = AV_NOPTS_VALUE;
+
+        pkt->size   = req_size;
+        pkt->flags |= AV_PKT_FLAG_KEY;
+        *got_packet = !!req_size;
     }
     return 0;
 }
 
 AVCodec ff_a64multi_encoder = {
     .name           = "a64multi",
+    .long_name      = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
     .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = CODEC_ID_A64_MULTI,
+    .id             = AV_CODEC_ID_A64_MULTI,
     .priv_data_size = sizeof(A64Context),
-    .init           = a64multi_init_encoder,
-    .encode         = a64multi_encode_frame,
+    .init           = a64multi_encode_init,
+    .encode2        = a64multi_encode_frame,
     .close          = a64multi_close_encoder,
-    .pix_fmts       = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
-    .long_name      = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
+    .pix_fmts       = (const enum AVPixelFormat[]) {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE},
     .capabilities   = CODEC_CAP_DELAY,
 };
 
 AVCodec ff_a64multi5_encoder = {
     .name           = "a64multi5",
+    .long_name      = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
     .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = CODEC_ID_A64_MULTI5,
+    .id             = AV_CODEC_ID_A64_MULTI5,
     .priv_data_size = sizeof(A64Context),
-    .init           = a64multi_init_encoder,
-    .encode         = a64multi_encode_frame,
+    .init           = a64multi_encode_init,
+    .encode2        = a64multi_encode_frame,
     .close          = a64multi_close_encoder,
-    .pix_fmts       = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
-    .long_name      = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
+    .pix_fmts       = (const enum AVPixelFormat[]) {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE},
     .capabilities   = CODEC_CAP_DELAY,
 };