1 /*****************************************************************************
2 * subtitle.c: subtitle decoder using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_codec.h>
34 #include <vlc_avcodec.h>
37 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
38 # include <libavcodec/avcodec.h>
39 # ifdef HAVE_AVCODEC_VAAPI
40 # include <libavcodec/vaapi.h>
42 #elif defined(HAVE_FFMPEG_AVCODEC_H)
43 # include <ffmpeg/avcodec.h>
50 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 25, 0 )
52 struct decoder_sys_t {
56 static subpicture_t *ConvertSubtitle(decoder_t *, AVSubtitle *, mtime_t pts);
59 * Initialize subtitle decoder
61 int InitSubtitleDec(decoder_t *dec, AVCodecContext *context,
62 AVCodec *codec, int codec_id, const char *namecodec)
68 case CODEC_ID_HDMV_PGS_SUBTITLE:
71 msg_Warn(dec, "refusing to decode non validated subtitle codec");
76 dec->p_sys = sys = malloc(sizeof(*sys));
80 codec->type = CODEC_TYPE_SUBTITLE;
81 context->codec_type = CODEC_TYPE_SUBTITLE;
82 context->codec_id = codec_id;
83 sys->p_context = context;
85 sys->i_codec_id = codec_id;
86 sys->psz_namecodec = namecodec;
87 sys->b_delayed_open = false;
90 context->extradata_size = 0;
91 context->extradata = NULL;
95 if (avcodec_open(context, codec) < 0) {
97 msg_Err(dec, "cannot open codec (%s)", namecodec);
98 free(context->extradata);
102 vlc_avcodec_unlock();
105 msg_Dbg(dec, "ffmpeg codec (%s) started", namecodec);
106 dec->fmt_out.i_cat = SPU_ES;
112 * Decode one subtitle
114 subpicture_t *DecodeSubtitle(decoder_t *dec, block_t **block_ptr)
116 decoder_sys_t *sys = dec->p_sys;
118 if (!block_ptr || !*block_ptr)
121 block_t *block = *block_ptr;
123 if (block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
124 block_Release(block);
125 avcodec_flush_buffers(sys->p_context);
129 if (block->i_buffer <= 0) {
130 block_Release(block);
135 block = block_Realloc(block,
137 block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE);
140 block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
141 memset(&block->p_buffer[block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE);
145 memset(&subtitle, 0, sizeof(subtitle));
148 av_init_packet(&pkt);
149 pkt.data = block->p_buffer;
150 pkt.size = block->i_buffer;
152 int has_subtitle = 0;
153 int used = avcodec_decode_subtitle2(sys->p_context,
154 &subtitle, &has_subtitle, &pkt);
157 msg_Warn(dec, "cannot decode one subtitle (%zu bytes)",
160 block_Release(block);
162 } else if ((size_t)used > block->i_buffer) {
163 used = block->i_buffer;
166 block->i_buffer -= used;
167 block->p_buffer += used;
170 subpicture_t *spu = NULL;
172 spu = ConvertSubtitle(dec, &subtitle,
173 block->i_pts > 0 ? block->i_pts : block->i_dts);
177 block_Release(block);
182 * Clean up private data
184 void EndSubtitleDec(decoder_t *dec)
190 * Convert a RGBA ffmpeg region to our format.
192 static subpicture_region_t *ConvertRegionRGBA(AVSubtitleRect *ffregion)
194 if (ffregion->w <= 0 || ffregion->h <= 0)
198 memset(&fmt, 0, sizeof(fmt));
199 fmt.i_chroma = VLC_FOURCC('R','G','B','A');
201 fmt.i_visible_width = ffregion->w;
203 fmt.i_visible_height = ffregion->h;
207 subpicture_region_t *region = subpicture_region_New(&fmt);
211 region->i_x = ffregion->x;
212 region->i_y = ffregion->y;
213 region->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
215 const plane_t *p = ®ion->p_picture->p[0];
216 for (int y = 0; y < ffregion->h; y++) {
217 for (int x = 0; x < ffregion->w; x++) {
218 /* I don't think don't have paletized RGB_A_ */
219 const uint8_t index = ffregion->pict.data[0][y * ffregion->w+x];
220 assert(index < ffregion->nb_colors);
223 memcpy(&color, &ffregion->pict.data[1][4*index], 4);
225 uint8_t *p_rgba = &p->p_pixels[y * p->i_pitch + x * p->i_pixel_pitch];
226 p_rgba[0] = (color >> 16) & 0xff;
227 p_rgba[1] = (color >> 8) & 0xff;
228 p_rgba[2] = (color >> 0) & 0xff;
229 p_rgba[3] = (color >> 24) & 0xff;
237 * Convert a ffmpeg subtitle to our format.
239 static subpicture_t *ConvertSubtitle(decoder_t *dec, AVSubtitle *ffsub, mtime_t pts)
241 subpicture_t *spu = decoder_NewSubpicture(dec, NULL);
245 //msg_Err(dec, "%lld %d %d",
246 // pts, ffsub->start_display_time, ffsub->end_display_time);
247 spu->i_start = pts + ffsub->start_display_time * INT64_C(1000);
248 spu->i_stop = pts + ffsub->end_display_time * INT64_C(1000);
249 spu->b_absolute = true; /* FIXME How to set it right ? */
250 spu->b_ephemer = true; /* FIXME How to set it right ? */
251 spu->i_original_picture_width =
252 dec->fmt_in.subs.spu.i_original_frame_width;
253 spu->i_original_picture_height =
254 dec->fmt_in.subs.spu.i_original_frame_height;
256 subpicture_region_t **region_next = &spu->p_region;
258 for (unsigned i = 0; i < ffsub->num_rects; i++) {
259 AVSubtitleRect *rec = ffsub->rects[i];
261 //msg_Err(dec, "SUBS RECT[%d]: %dx%d @%dx%d",
262 // i, rec->w, rec->h, rec->x, rec->y);
264 subpicture_region_t *region = NULL;
265 switch (ffsub->format) {
267 region = ConvertRegionRGBA(rec);
270 msg_Warn(dec, "unsupported subtitle type");
275 *region_next = region;
276 region_next = ®ion->p_next;
278 /* Free AVSubtitleRect */
279 avpicture_free(&rec->pict);
282 av_free(ffsub->rects);