subtitle.c 8.52 KB
Newer Older
1
/*****************************************************************************
2
 * subtitle.c: subtitle decoder using libavcodec library
3 4 5 6 7 8
 *****************************************************************************
 * Copyright (C) 2009 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * 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
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * 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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>

#include <vlc_common.h>
#include <vlc_codec.h>
#include <vlc_avcodec.h>

36
#include <libavcodec/avcodec.h>
37
#include <libavutil/mem.h>
38 39 40 41

#include "avcodec.h"

struct decoder_sys_t {
42
    AVCODEC_COMMON_MEMBERS
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
43
    bool b_need_ephemer; /* Does the format need the ephemer flag (no end time set) */
44 45
};

46 47
static subpicture_t *ConvertSubtitle(decoder_t *, AVSubtitle *, mtime_t pts,
                                     AVCodecContext *avctx);
48
static subpicture_t *DecodeSubtitle(decoder_t *, block_t **);
49 50 51 52 53

/**
 * Initialize subtitle decoder
 */
int InitSubtitleDec(decoder_t *dec, AVCodecContext *context,
54
                    const AVCodec *codec)
55 56 57
{
    decoder_sys_t *sys;

58
    /* */
59
    switch (codec->id) {
60 61
    case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
    case AV_CODEC_ID_XSUB:
Rafaël Carré's avatar
Rafaël Carré committed
62
    case AV_CODEC_ID_DVB_SUBTITLE:
63 64 65 66 67 68
        break;
    default:
        msg_Warn(dec, "refusing to decode non validated subtitle codec");
        return VLC_EGENERIC;
    }

69 70 71 72 73 74 75 76
    /* */
    dec->p_sys = sys = malloc(sizeof(*sys));
    if (!sys)
        return VLC_ENOMEM;

    sys->p_context = context;
    sys->p_codec = codec;
    sys->b_delayed_open = false;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
77
    sys->b_need_ephemer = codec->id == AV_CODEC_ID_HDMV_PGS_SUBTITLE;
78 79 80 81 82 83

    /* */
    context->extradata_size = 0;
    context->extradata = NULL;

    /* */
84
    int ret;
85 86 87 88 89 90
    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);

91
    vlc_avcodec_lock();
92
    ret = avcodec_open2(context, codec, options ? &options : NULL);
93
    vlc_avcodec_unlock();
94 95 96 97 98 99 100

    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);

101
    if (ret < 0) {
102
        msg_Err(dec, "cannot open codec (%s)", codec->name);
103 104 105 106 107
        free(sys);
        return VLC_EGENERIC;
    }

    /* */
108
    msg_Dbg(dec, "libavcodec codec (%s) started", codec->name);
109
    dec->fmt_out.i_cat = SPU_ES;
110
    dec->pf_decode_sub = DecodeSubtitle;
111 112 113 114 115 116 117

    return VLC_SUCCESS;
}

/**
 * Decode one subtitle
 */
118
static subpicture_t *DecodeSubtitle(decoder_t *dec, block_t **block_ptr)
119 120 121 122 123 124 125 126
{
    decoder_sys_t *sys = dec->p_sys;

    if (!block_ptr || !*block_ptr)
        return NULL;

    block_t *block = *block_ptr;

127
    if (block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
128 129 130
        block_Release(block);
        avcodec_flush_buffers(sys->p_context);
        return NULL;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    }

    if (block->i_buffer <= 0) {
        block_Release(block);
        return NULL;
    }

    *block_ptr =
    block      = block_Realloc(block,
                               0,
                               block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE);
    if (!block)
        return NULL;
    block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
    memset(&block->p_buffer[block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE);

    /* */
    AVSubtitle subtitle;
    memset(&subtitle, 0, sizeof(subtitle));

    AVPacket pkt;
    av_init_packet(&pkt);
    pkt.data = block->p_buffer;
    pkt.size = block->i_buffer;

    int has_subtitle = 0;
    int used = avcodec_decode_subtitle2(sys->p_context,
                                        &subtitle, &has_subtitle, &pkt);

    if (used < 0) {
        msg_Warn(dec, "cannot decode one subtitle (%zu bytes)",
                 block->i_buffer);

        block_Release(block);
        return NULL;
    } else if ((size_t)used > block->i_buffer) {
        used = block->i_buffer;
    }

    block->i_buffer -= used;
    block->p_buffer += used;

    /* */
    subpicture_t *spu = NULL;
    if (has_subtitle)
        spu = ConvertSubtitle(dec, &subtitle,
177 178
                              block->i_pts > 0 ? block->i_pts : block->i_dts,
                              sys->p_context);
179 180 181 182 183 184 185 186

    /* */
    if (!spu)
        block_Release(block);
    return spu;
}

/**
187
 * Convert a RGBA libavcodec region to our format.
188 189 190
 */
static subpicture_region_t *ConvertRegionRGBA(AVSubtitleRect *ffregion)
{
191 192
    if (ffregion->w <= 0 || ffregion->h <= 0)
        return NULL;
193

194
    video_format_t fmt;
195
    memset(&fmt, 0, sizeof(fmt));
Ilkka Ollakka's avatar
Ilkka Ollakka committed
196
    fmt.i_chroma         = VLC_CODEC_RGBA;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    fmt.i_width          =
    fmt.i_visible_width  = ffregion->w;
    fmt.i_height         =
    fmt.i_visible_height = ffregion->h;
    fmt.i_x_offset       = 0;
    fmt.i_y_offset       = 0;

    subpicture_region_t *region = subpicture_region_New(&fmt);
    if (!region)
        return NULL;

    region->i_x = ffregion->x;
    region->i_y = ffregion->y;
    region->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;

    const plane_t *p = &region->p_picture->p[0];
    for (int y = 0; y < ffregion->h; y++) {
        for (int x = 0; x < ffregion->w; x++) {
            /* I don't think don't have paletized RGB_A_ */
            const uint8_t index = ffregion->pict.data[0][y * ffregion->w+x];
            assert(index < ffregion->nb_colors);

            uint32_t color;
            memcpy(&color, &ffregion->pict.data[1][4*index], 4);

            uint8_t *p_rgba = &p->p_pixels[y * p->i_pitch + x * p->i_pixel_pitch];
            p_rgba[0] = (color >> 16) & 0xff;
            p_rgba[1] = (color >>  8) & 0xff;
            p_rgba[2] = (color >>  0) & 0xff;
            p_rgba[3] = (color >> 24) & 0xff;
        }
    }

    return region;
}

/**
234
 * Convert a libavcodec subtitle to our format.
235
 */
236 237
static subpicture_t *ConvertSubtitle(decoder_t *dec, AVSubtitle *ffsub, mtime_t pts,
                                     AVCodecContext *avctx)
238
{
239
    subpicture_t *spu = decoder_NewSubpicture(dec, NULL);
240 241 242 243 244 245 246
    if (!spu)
        return NULL;

    //msg_Err(dec, "%lld %d %d",
    //        pts, ffsub->start_display_time, ffsub->end_display_time);
    spu->i_start    = pts + ffsub->start_display_time * INT64_C(1000);
    spu->i_stop     = pts + ffsub->end_display_time * INT64_C(1000);
247
    spu->b_absolute = true; /* We have offset and size for subtitle */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
248 249
    spu->b_ephemer  = dec->p_sys->b_need_ephemer;
                    /* We only show subtitle for i_stop time only */
250

251
    if (avctx->coded_width != 0 && avctx->coded_height != 0) {
252 253 254 255 256 257 258 259
        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;
    }
260 261 262 263 264 265

    subpicture_region_t **region_next = &spu->p_region;

    for (unsigned i = 0; i < ffsub->num_rects; i++) {
        AVSubtitleRect *rec = ffsub->rects[i];

266 267
        //msg_Err(dec, "SUBS RECT[%d]: %dx%d @%dx%d",
        //         i, rec->w, rec->h, rec->x, rec->y);
268

269
        subpicture_region_t *region = NULL;
270 271 272 273 274 275 276 277 278 279 280 281 282
        switch (ffsub->format) {
        case 0:
            region = ConvertRegionRGBA(rec);
            break;
        default:
            msg_Warn(dec, "unsupported subtitle type");
            region = NULL;
            break;
        }
        if (region) {
            *region_next = region;
            region_next = &region->p_next;
        }
283 284 285
        /* Free AVSubtitleRect */
        avpicture_free(&rec->pict);
        av_free(rec);
286
    }
287
    av_free(ffsub->rects);
288 289 290 291

    return spu;
}