FFmpeg
Loading...
Searching...
No Matches
srtdec.c
Go to the documentation of this file.
1/*
2 * SubRip subtitle decoder
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "config_components.h"
23
24#include "libavutil/avstring.h"
25#include "libavutil/common.h"
28#include "avcodec.h"
29#include "ass.h"
30#include "codec_internal.h"
31#include "decode.h"
32#include "htmlsubtitles.h"
33
34static int srt_to_ass(AVCodecContext *avctx, AVBPrint *dst,
35 const char *in, int x1, int y1, int x2, int y2)
36{
37 if (x1 >= 0 && y1 >= 0) {
38 /* XXX: here we rescale coordinate assuming they are in DVD resolution
39 * (720x480) since we don't have anything better */
40
41 if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1) && x2 >= x1 && y2 >= y1) {
42 /* text rectangle defined, write the text at the center of the rectangle */
43 const int cx = x1 + (x2 - x1)/2;
44 const int cy = y1 + (y2 - y1)/2;
45 const int scaled_x = cx * (int64_t)ASS_DEFAULT_PLAYRESX / 720;
46 const int scaled_y = cy * (int64_t)ASS_DEFAULT_PLAYRESY / 480;
47 av_bprintf(dst, "{\\an5}{\\pos(%d,%d)}", scaled_x, scaled_y);
48 } else {
49 /* only the top left corner, assume the text starts in that corner */
50 const int scaled_x = x1 * (int64_t)ASS_DEFAULT_PLAYRESX / 720;
51 const int scaled_y = y1 * (int64_t)ASS_DEFAULT_PLAYRESY / 480;
52 av_bprintf(dst, "{\\an1}{\\pos(%d,%d)}", scaled_x, scaled_y);
53 }
54 }
55
56 return ff_htmlmarkup_to_ass(avctx, dst, in);
57}
58
60 int *got_sub_ptr, const AVPacket *avpkt)
61{
62 AVBPrint buffer;
63 int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
64 int ret;
65 size_t size;
68
69 if (p && size == 16) {
70 x1 = AV_RL32(p );
71 y1 = AV_RL32(p + 4);
72 x2 = AV_RL32(p + 8);
73 y2 = AV_RL32(p + 12);
74 }
75
76 if (avpkt->size <= 0)
77 return avpkt->size;
78
79 const char *text = ff_decode_subtitle_text(avctx, avpkt);
80 if (!text)
81 return AVERROR(ENOMEM);
82
84
85 ret = srt_to_ass(avctx, &buffer, text, x1, y1, x2, y2);
86 if (ret >= 0)
87 ret = ff_ass_add_rect(sub, buffer.str, s->readorder++, 0, NULL, NULL);
89 if (ret < 0)
90 return ret;
91
92 *got_sub_ptr = sub->num_rects > 0;
93 return avpkt->size;
94}
95
96#if CONFIG_SRT_DECODER
97/* deprecated decoder */
98const FFCodec ff_srt_decoder = {
99 .p.name = "srt",
100 CODEC_LONG_NAME("SubRip subtitle"),
101 .p.type = AVMEDIA_TYPE_SUBTITLE,
102 .p.id = AV_CODEC_ID_SUBRIP,
105 .flush = ff_ass_decoder_flush,
106 .priv_data_size = sizeof(FFASSDecoderContext),
107};
108#endif
109
110#if CONFIG_SUBRIP_DECODER
112 .p.name = "subrip",
113 CODEC_LONG_NAME("SubRip subtitle"),
114 .p.type = AVMEDIA_TYPE_SUBTITLE,
115 .p.id = AV_CODEC_ID_SUBRIP,
118 .flush = ff_ass_decoder_flush,
119 .priv_data_size = sizeof(FFASSDecoderContext),
120};
121#endif
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
const FFCodec ff_subrip_decoder
const FFCodec ff_srt_decoder
av_cold void ff_ass_decoder_flush(AVCodecContext *avctx)
Helper to flush a text subtitles decoder making use of the FFASSDecoderContext.
Definition ass.c:166
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition ass.c:98
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int readorder, int layer, const char *style, const char *speaker)
Add an ASS dialog to a subtitle.
Definition ass.c:159
#define ASS_DEFAULT_PLAYRESY
Definition ass.h:29
#define ASS_DEFAULT_PLAYRESX
Definition ass.h:28
Libavcodec external API header.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:121
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
#define AV_BPRINT_SIZE_UNLIMITED
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_LONG_NAME(str)
#define FF_CODEC_DECODE_SUB_CB(func)
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
const char * ff_decode_subtitle_text(AVCodecContext *avctx, const AVPacket *avpkt)
Get the data of a subtitle packet followed by a 0 byte.
Definition decode.c:943
int ret
Definition ffplay.c:391
@ AV_CODEC_ID_SUBRIP
Definition codec_id.h:589
@ AV_PKT_DATA_SUBTITLE_POSITION
Subtitle event position.
Definition packet.h:180
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
#define AV_RL32(p)
static int srt_decode_frame(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *avpkt)
Definition srtdec.c:59
static int srt_to_ass(AVCodecContext *avctx, AVBPrint *dst, const char *in, int x1, int y1, int x2, int y2)
Definition srtdec.c:34
misc parsing utilities
main external API structure.
Definition avcodec.h:443
void * priv_data
Definition avcodec.h:470
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
unsigned num_rects
Definition avcodec.h:2111
static char buffer[20]
Definition seek.c:33
int size