]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/subtitle.c
Avcodec: we already require 52.25.0
[vlc] / modules / codec / avcodec / subtitle.c
1 /*****************************************************************************
2  * subtitle.c: subtitle decoder using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 #include <assert.h>
31
32 #include <vlc_common.h>
33 #include <vlc_codec.h>
34 #include <vlc_avcodec.h>
35
36 /* ffmpeg header */
37 #include <libavcodec/avcodec.h>
38 #ifdef HAVE_AVCODEC_VAAPI
39 #    include <libavcodec/vaapi.h>
40 #endif
41
42 #include "avcodec.h"
43
44 struct decoder_sys_t {
45     AVCODEC_COMMON_MEMBERS
46 };
47
48 static subpicture_t *ConvertSubtitle(decoder_t *, AVSubtitle *, mtime_t pts);
49
50 /**
51  * Initialize subtitle decoder
52  */
53 int InitSubtitleDec(decoder_t *dec, AVCodecContext *context,
54                     AVCodec *codec, int codec_id, const char *namecodec)
55 {
56     decoder_sys_t *sys;
57
58     /* */
59     switch (codec_id) {
60     case CODEC_ID_HDMV_PGS_SUBTITLE:
61     case CODEC_ID_XSUB:
62         break;
63     default:
64         msg_Warn(dec, "refusing to decode non validated subtitle codec");
65         return VLC_EGENERIC;
66     }
67
68     /* */
69     dec->p_sys = sys = malloc(sizeof(*sys));
70     if (!sys)
71         return VLC_ENOMEM;
72
73     codec->type = AVMEDIA_TYPE_SUBTITLE;
74     context->codec_type = AVMEDIA_TYPE_SUBTITLE;
75     context->codec_id = codec_id;
76     sys->p_context = context;
77     sys->p_codec = codec;
78     sys->i_codec_id = codec_id;
79     sys->psz_namecodec = namecodec;
80     sys->b_delayed_open = false;
81
82     /* */
83     context->extradata_size = 0;
84     context->extradata = NULL;
85
86     /* */
87     int ret;
88     vlc_avcodec_lock();
89 #if LIBAVCODEC_VERSION_MAJOR < 54
90     ret = avcodec_open(context, codec);
91 #else
92     ret = avcodec_open2(context, codec, NULL /* options */);
93 #endif
94     vlc_avcodec_unlock();
95     if (ret < 0) {
96         msg_Err(dec, "cannot open codec (%s)", namecodec);
97         free(context->extradata);
98         free(sys);
99         return VLC_EGENERIC;
100     }
101
102     /* */
103     msg_Dbg(dec, "ffmpeg codec (%s) started", namecodec);
104     dec->fmt_out.i_cat = SPU_ES;
105
106     return VLC_SUCCESS;
107 }
108
109 /**
110  * Decode one subtitle
111  */
112 subpicture_t *DecodeSubtitle(decoder_t *dec, block_t **block_ptr)
113 {
114     decoder_sys_t *sys = dec->p_sys;
115
116     if (!block_ptr || !*block_ptr)
117         return NULL;
118
119     block_t *block = *block_ptr;
120
121     if (block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
122         block_Release(block);
123         avcodec_flush_buffers(sys->p_context);
124         return NULL;
125     }
126
127     if (block->i_buffer <= 0) {
128         block_Release(block);
129         return NULL;
130     }
131
132     *block_ptr =
133     block      = block_Realloc(block,
134                                0,
135                                block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE);
136     if (!block)
137         return NULL;
138     block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
139     memset(&block->p_buffer[block->i_buffer], 0, FF_INPUT_BUFFER_PADDING_SIZE);
140
141     /* */
142     AVSubtitle subtitle;
143     memset(&subtitle, 0, sizeof(subtitle));
144
145     AVPacket pkt;
146     av_init_packet(&pkt);
147     pkt.data = block->p_buffer;
148     pkt.size = block->i_buffer;
149
150     int has_subtitle = 0;
151     int used = avcodec_decode_subtitle2(sys->p_context,
152                                         &subtitle, &has_subtitle, &pkt);
153
154     if (used < 0) {
155         msg_Warn(dec, "cannot decode one subtitle (%zu bytes)",
156                  block->i_buffer);
157
158         block_Release(block);
159         return NULL;
160     } else if ((size_t)used > block->i_buffer) {
161         used = block->i_buffer;
162     }
163
164     block->i_buffer -= used;
165     block->p_buffer += used;
166
167     /* */
168     subpicture_t *spu = NULL;
169     if (has_subtitle)
170         spu = ConvertSubtitle(dec, &subtitle,
171                               block->i_pts > 0 ? block->i_pts : block->i_dts);
172
173     /* */
174     if (!spu)
175         block_Release(block);
176     return spu;
177 }
178
179 /**
180  * Clean up private data
181  */
182 void EndSubtitleDec(decoder_t *dec)
183 {
184     VLC_UNUSED(dec);
185 }
186
187 /**
188  * Convert a RGBA ffmpeg region to our format.
189  */
190 static subpicture_region_t *ConvertRegionRGBA(AVSubtitleRect *ffregion)
191 {
192     if (ffregion->w <= 0 || ffregion->h <= 0)
193         return NULL;
194
195     video_format_t fmt;
196     memset(&fmt, 0, sizeof(fmt));
197     fmt.i_chroma         = VLC_FOURCC('R','G','B','A');
198     fmt.i_width          =
199     fmt.i_visible_width  = ffregion->w;
200     fmt.i_height         =
201     fmt.i_visible_height = ffregion->h;
202     fmt.i_x_offset       = 0;
203     fmt.i_y_offset       = 0;
204
205     subpicture_region_t *region = subpicture_region_New(&fmt);
206     if (!region)
207         return NULL;
208
209     region->i_x = ffregion->x;
210     region->i_y = ffregion->y;
211     region->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
212
213     const plane_t *p = &region->p_picture->p[0];
214     for (int y = 0; y < ffregion->h; y++) {
215         for (int x = 0; x < ffregion->w; x++) {
216             /* I don't think don't have paletized RGB_A_ */
217             const uint8_t index = ffregion->pict.data[0][y * ffregion->w+x];
218             assert(index < ffregion->nb_colors);
219
220             uint32_t color;
221             memcpy(&color, &ffregion->pict.data[1][4*index], 4);
222
223             uint8_t *p_rgba = &p->p_pixels[y * p->i_pitch + x * p->i_pixel_pitch];
224             p_rgba[0] = (color >> 16) & 0xff;
225             p_rgba[1] = (color >>  8) & 0xff;
226             p_rgba[2] = (color >>  0) & 0xff;
227             p_rgba[3] = (color >> 24) & 0xff;
228         }
229     }
230
231     return region;
232 }
233
234 /**
235  * Convert a ffmpeg subtitle to our format.
236  */
237 static subpicture_t *ConvertSubtitle(decoder_t *dec, AVSubtitle *ffsub, mtime_t pts)
238 {
239     subpicture_t *spu = decoder_NewSubpicture(dec, NULL);
240     if (!spu)
241         return NULL;
242
243     //msg_Err(dec, "%lld %d %d",
244     //        pts, ffsub->start_display_time, ffsub->end_display_time);
245     spu->i_start    = pts + ffsub->start_display_time * INT64_C(1000);
246     spu->i_stop     = pts + ffsub->end_display_time * INT64_C(1000);
247     spu->b_absolute = true; /* FIXME How to set it right ? */
248     spu->b_ephemer  = true; /* FIXME How to set it right ? */
249     spu->i_original_picture_width =
250         dec->fmt_in.subs.spu.i_original_frame_width;
251     spu->i_original_picture_height =
252         dec->fmt_in.subs.spu.i_original_frame_height;
253
254     subpicture_region_t **region_next = &spu->p_region;
255
256     for (unsigned i = 0; i < ffsub->num_rects; i++) {
257         AVSubtitleRect *rec = ffsub->rects[i];
258
259         //msg_Err(dec, "SUBS RECT[%d]: %dx%d @%dx%d",
260         //         i, rec->w, rec->h, rec->x, rec->y);
261
262         subpicture_region_t *region = NULL;
263         switch (ffsub->format) {
264         case 0:
265             region = ConvertRegionRGBA(rec);
266             break;
267         default:
268             msg_Warn(dec, "unsupported subtitle type");
269             region = NULL;
270             break;
271         }
272         if (region) {
273             *region_next = region;
274             region_next = &region->p_next;
275         }
276         /* Free AVSubtitleRect */
277         avpicture_free(&rec->pict);
278         av_free(rec);
279     }
280     av_free(ffsub->rects);
281
282     return spu;
283 }
284