FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
assdec.c
Go to the documentation of this file.
1 /*
2  * SSA/ASS 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 <string.h>
23 
24 #include "avcodec.h"
25 #include "ass.h"
26 #include "ass_split.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mem.h"
29 
31 {
32  avctx->subtitle_header = av_malloc(avctx->extradata_size + 1);
33  if (!avctx->subtitle_header)
34  return AVERROR(ENOMEM);
35  if (avctx->extradata_size)
36  memcpy(avctx->subtitle_header, avctx->extradata, avctx->extradata_size);
37  avctx->subtitle_header[avctx->extradata_size] = 0;
38  avctx->subtitle_header_size = avctx->extradata_size;
39  avctx->priv_data = ff_ass_split(avctx->extradata);
40  if(!avctx->priv_data)
41  return -1;
42  return 0;
43 }
44 
45 static int ass_decode_close(AVCodecContext *avctx)
46 {
48  avctx->priv_data = NULL;
49  return 0;
50 }
51 
52 #if CONFIG_SSA_DECODER
53 static int ssa_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr,
54  AVPacket *avpkt)
55 {
56  const char *ptr = avpkt->data;
57  int len, size = avpkt->size;
58 
59  while (size > 0) {
60  int duration;
61  ASSDialog *dialog = ff_ass_split_dialog(avctx->priv_data, ptr, 0, NULL);
62  if (!dialog)
63  return AVERROR_INVALIDDATA;
64  duration = dialog->end - dialog->start;
65  len = ff_ass_add_rect(data, ptr, 0, duration, 1);
66  if (len < 0)
67  return len;
68  ptr += len;
69  size -= len;
70  }
71 
72  *got_sub_ptr = avpkt->size > 0;
73  return avpkt->size;
74 }
75 
76 AVCodec ff_ssa_decoder = {
77  .name = "ssa",
78  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
79  .type = AVMEDIA_TYPE_SUBTITLE,
80  .id = AV_CODEC_ID_SSA,
81  .init = ass_decode_init,
82  .decode = ssa_decode_frame,
83  .close = ass_decode_close,
84 };
85 #endif
86 
87 #if CONFIG_ASS_DECODER
88 static int ass_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr,
89  AVPacket *avpkt)
90 {
91  int ret;
92  AVSubtitle *sub = data;
93  const char *ptr = avpkt->data;
94  static const AVRational ass_tb = {1, 100};
95  const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, ass_tb);
96  const int ts_duration = av_rescale_q(avpkt->duration, avctx->time_base, ass_tb);
97 
98  if (avpkt->size <= 0)
99  return avpkt->size;
100 
101  ret = ff_ass_add_rect(sub, ptr, ts_start, ts_duration, 2);
102  if (ret < 0) {
103  if (ret == AVERROR_INVALIDDATA)
104  av_log(avctx, AV_LOG_ERROR, "Invalid ASS packet\n");
105  return ret;
106  }
107 
108  *got_sub_ptr = avpkt->size > 0;
109  return avpkt->size;
110 }
111 
112 AVCodec ff_ass_decoder = {
113  .name = "ass",
114  .long_name = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
115  .type = AVMEDIA_TYPE_SUBTITLE,
116  .id = AV_CODEC_ID_ASS,
117  .init = ass_decode_init,
118  .decode = ass_decode_frame,
119  .close = ass_decode_close,
120 };
121 #endif
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
fields extracted from the [Events] section
Definition: ass_split.h:71
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
memory handling functions
ASSSplitContext * ff_ass_split(const char *buf)
Split a full ASS file or a ASS header from a string buffer and store the split structure in a newly a...
Definition: ass_split.c:356
int size
Definition: avcodec.h:1434
AVCodec.
Definition: avcodec.h:3482
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1641
static av_cold int ass_decode_init(AVCodecContext *avctx)
Definition: assdec.c:30
#define av_cold
Definition: attributes.h:74
#define av_malloc(s)
int subtitle_header_size
Definition: avcodec.h:3247
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
uint8_t * data
Definition: avcodec.h:1433
ptrdiff_t size
Definition: opengl_enc.c:101
static int64_t duration
Definition: ffplay.c:326
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1451
ASSDialog * ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf, int cache, int *number)
Split one or several ASS "Dialogue" lines from a string buffer and store them in a already initialize...
Definition: ass_split.c:393
#define av_log(a,...)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:147
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
int end
end time of the dialog in centiseconds
Definition: ass_split.h:74
Libavcodec external API header.
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int ts_start, int duration, int raw)
Add an ASS dialog line to an AVSubtitle as a new AVSubtitleRect.
Definition: ass.c:144
common internal API header
main external API structure.
Definition: avcodec.h:1512
int extradata_size
Definition: avcodec.h:1628
int start
start time of the dialog in centiseconds
Definition: ass_split.h:73
rational number numerator/denominator
Definition: rational.h:43
static int ass_decode_close(AVCodecContext *avctx)
Definition: assdec.c:45
ASS as defined in Matroska.
Definition: avcodec.h:532
void * priv_data
Definition: avcodec.h:1554
int len
void ff_ass_split_free(ASSSplitContext *ctx)
Free all the memory allocated for an ASSSplitContext.
Definition: ass_split.c:412
This structure stores compressed data.
Definition: avcodec.h:1410
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:3246