]> git.sesse.net Git - vlc/blobdiff - modules/codec/avcodec/subtitle.c
modules: Remove use of gcc specific conditionals with omitted operands
[vlc] / modules / codec / avcodec / subtitle.c
index 1e8047b1a98e318e3e9f01e8ec3278b9964ea275..fa476f0c778e332701bc0188d6ed218c67a63d0f 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
- * subtitle.c: subtitle decoder using ffmpeg library
+ * subtitle.c: subtitle decoder using libavcodec library
  *****************************************************************************
  * Copyright (C) 2009 Laurent Aimar
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program 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.
  *
  * This program 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 General Public License for more details.
+ * 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 General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <vlc_common.h>
 #include <vlc_codec.h>
 #include <vlc_avcodec.h>
-#include <vlc_osd.h>
-
-/* ffmpeg header */
-#ifdef HAVE_LIBAVCODEC_AVCODEC_H
-#   include <libavcodec/avcodec.h>
-#   ifdef HAVE_AVCODEC_VAAPI
-#       include <libavcodec/vaapi.h>
-#   endif
-#elif defined(HAVE_FFMPEG_AVCODEC_H)
-#   include <ffmpeg/avcodec.h>
-#else
-#   include <avcodec.h>
-#endif
 
-#include "avcodec.h"
+#include <libavcodec/avcodec.h>
+#include <libavutil/mem.h>
 
-#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 25, 0 )
+#include "avcodec.h"
 
 struct decoder_sys_t {
-    FFMPEG_COMMON_MEMBERS
+    AVCODEC_COMMON_MEMBERS
 };
 
-static subpicture_t *ConvertSubtitle(decoder_t *, AVSubtitle *, mtime_t pts);
+static subpicture_t *ConvertSubtitle(decoder_t *, AVSubtitle *, mtime_t pts,
+                                     AVCodecContext *avctx);
+static subpicture_t *DecodeSubtitle(decoder_t *, block_t **);
 
 /**
  * Initialize subtitle decoder
  */
 int InitSubtitleDec(decoder_t *dec, AVCodecContext *context,
-                    AVCodec *codec, int codec_id, const char *namecodec)
+                    const AVCodec *codec)
 {
     decoder_sys_t *sys;
 
+    /* */
+    switch (codec->id) {
+    case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
+    case AV_CODEC_ID_XSUB:
+    case AV_CODEC_ID_DVB_SUBTITLE:
+        break;
+    default:
+        msg_Warn(dec, "refusing to decode non validated subtitle codec");
+        return VLC_EGENERIC;
+    }
+
     /* */
     dec->p_sys = sys = malloc(sizeof(*sys));
     if (!sys)
@@ -71,8 +72,6 @@ int InitSubtitleDec(decoder_t *dec, AVCodecContext *context,
 
     sys->p_context = context;
     sys->p_codec = codec;
-    sys->i_codec_id = codec_id;
-    sys->psz_namecodec = namecodec;
     sys->b_delayed_open = false;
 
     /* */
@@ -80,19 +79,34 @@ int InitSubtitleDec(decoder_t *dec, AVCodecContext *context,
     context->extradata = NULL;
 
     /* */
+    int ret;
+    char *psz_opts = var_InheritString(dec, "avcodec-options");
+    AVDictionary *options = NULL;
+    if (psz_opts && *psz_opts)
+        options = vlc_av_get_options(psz_opts);
+    free(psz_opts);
+
     vlc_avcodec_lock();
-    if (avcodec_open(context, codec) < 0) {
-        vlc_avcodec_unlock();
-        msg_Err(dec, "cannot open codec (%s)", namecodec);
+    ret = avcodec_open2(context, codec, options ? &options : NULL);
+    vlc_avcodec_unlock();
+
+    AVDictionaryEntry *t = NULL;
+    while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
+        msg_Err(dec, "Unknown option \"%s\"", t->key);
+    }
+    av_dict_free(&options);
+
+    if (ret < 0) {
+        msg_Err(dec, "cannot open codec (%s)", codec->name);
         free(context->extradata);
         free(sys);
         return VLC_EGENERIC;
     }
-    vlc_avcodec_unlock();
 
     /* */
-    msg_Dbg(dec, "ffmpeg codec (%s) started", namecodec);
+    msg_Dbg(dec, "libavcodec codec (%s) started", codec->name);
     dec->fmt_out.i_cat = SPU_ES;
+    dec->pf_decode_sub = DecodeSubtitle;
 
     return VLC_SUCCESS;
 }
@@ -100,7 +114,7 @@ int InitSubtitleDec(decoder_t *dec, AVCodecContext *context,
 /**
  * Decode one subtitle
  */
-subpicture_t *DecodeSubtitle(decoder_t *dec, block_t **block_ptr)
+static subpicture_t *DecodeSubtitle(decoder_t *dec, block_t **block_ptr)
 {
     decoder_sys_t *sys = dec->p_sys;
 
@@ -159,7 +173,8 @@ subpicture_t *DecodeSubtitle(decoder_t *dec, block_t **block_ptr)
     subpicture_t *spu = NULL;
     if (has_subtitle)
         spu = ConvertSubtitle(dec, &subtitle,
-                              block->i_pts > 0 ? block->i_pts : block->i_dts);
+                              block->i_pts > 0 ? block->i_pts : block->i_dts,
+                              sys->p_context);
 
     /* */
     if (!spu)
@@ -168,23 +183,16 @@ subpicture_t *DecodeSubtitle(decoder_t *dec, block_t **block_ptr)
 }
 
 /**
- * Clean up private data
- */
-void EndSubtitleDec(decoder_t *dec)
-{
-    VLC_UNUSED(dec);
-}
-
-/**
- * Convert a RGBA ffmpeg region to our format.
+ * Convert a RGBA libavcodec region to our format.
  */
 static subpicture_region_t *ConvertRegionRGBA(AVSubtitleRect *ffregion)
 {
-    video_format_t fmt;
+    if (ffregion->w <= 0 || ffregion->h <= 0)
+        return NULL;
 
+    video_format_t fmt;
     memset(&fmt, 0, sizeof(fmt));
-    fmt.i_chroma         = VLC_FOURCC('R','G','B','A');
-    fmt.i_aspect         = 0;
+    fmt.i_chroma         = VLC_CODEC_RGBA;
     fmt.i_width          =
     fmt.i_visible_width  = ffregion->w;
     fmt.i_height         =
@@ -222,11 +230,12 @@ static subpicture_region_t *ConvertRegionRGBA(AVSubtitleRect *ffregion)
 }
 
 /**
- * Convert a ffmpeg subtitle to our format.
+ * Convert a libavcodec subtitle to our format.
  */
-static subpicture_t *ConvertSubtitle(decoder_t *dec, AVSubtitle *ffsub, mtime_t pts)
+static subpicture_t *ConvertSubtitle(decoder_t *dec, AVSubtitle *ffsub, mtime_t pts,
+                                     AVCodecContext *avctx)
 {
-    subpicture_t *spu = decoder_NewSubpicture(dec);
+    subpicture_t *spu = decoder_NewSubpicture(dec, NULL);
     if (!spu)
         return NULL;
 
@@ -236,20 +245,26 @@ static subpicture_t *ConvertSubtitle(decoder_t *dec, AVSubtitle *ffsub, mtime_t
     spu->i_stop     = pts + ffsub->end_display_time * INT64_C(1000);
     spu->b_absolute = true; /* FIXME How to set it right ? */
     spu->b_ephemer  = true; /* FIXME How to set it right ? */
-    spu->i_original_picture_width =
-        dec->fmt_in.subs.spu.i_original_frame_width;
-    spu->i_original_picture_height =
-        dec->fmt_in.subs.spu.i_original_frame_height;
+
+    if (avctx->coded_width != 0 && avctx->coded_height != 0) {
+        spu->i_original_picture_width = avctx->coded_width;
+        spu->i_original_picture_height = avctx->coded_height;
+    } else {
+        spu->i_original_picture_width =
+            dec->fmt_in.subs.spu.i_original_frame_width;
+        spu->i_original_picture_height =
+            dec->fmt_in.subs.spu.i_original_frame_height;
+    }
 
     subpicture_region_t **region_next = &spu->p_region;
 
     for (unsigned i = 0; i < ffsub->num_rects; i++) {
         AVSubtitleRect *rec = ffsub->rects[i];
 
-        msg_Err(dec, "SUBS RECT[%d]: %dx%d @%dx%d",
-                 i, rec->w, rec->h, rec->x, rec->y);
+        //msg_Err(dec, "SUBS RECT[%d]: %dx%d @%dx%d",
+        //         i, rec->w, rec->h, rec->x, rec->y);
 
-        subpicture_region_t *region;
+        subpicture_region_t *region = NULL;
         switch (ffsub->format) {
         case 0:
             region = ConvertRegionRGBA(rec);
@@ -263,15 +278,12 @@ static subpicture_t *ConvertSubtitle(decoder_t *dec, AVSubtitle *ffsub, mtime_t
             *region_next = region;
             region_next = &region->p_next;
         }
-        /* Free AVSubtitleRect
-         * FIXME isn't there an avcodec function ? */
-        free(rec->pict.data[0]); /* Plane */
-        free(rec->pict.data[1]); /* Palette */
-        free(rec);
+        /* Free AVSubtitleRect */
+        avpicture_free(&rec->pict);
+        av_free(rec);
     }
-    free(ffsub->rects);
+    av_free(ffsub->rects);
 
     return spu;
 }
 
-#endif