@@ -297,6 +297,7 @@ OBJS-$(CONFIG_H264_QSV_DECODER) += qsvdec_h2645.o
OBJS-$(CONFIG_H264_QSV_ENCODER) += qsvenc_h264.o
OBJS-$(CONFIG_HAP_DECODER) += hapdec.o hap.o
OBJS-$(CONFIG_HAP_ENCODER) += hapenc.o hap.o
+OBJS-$(CONFIG_HDMV_TEXTST_DECODER) += hdmv_textstdec.o ass.o
OBJS-$(CONFIG_HEVC_DECODER) += hevc.o hevc_mvs.o hevc_ps.o hevc_sei.o \
hevc_cabac.o hevc_refs.o hevcpred.o \
hevcdsp.o hevc_filter.o hevc_parse.o hevc_data.o
@@ -526,6 +526,7 @@ void avcodec_register_all(void)
REGISTER_DECODER(CCAPTION, ccaption);
REGISTER_ENCDEC (DVBSUB, dvbsub);
REGISTER_ENCDEC (DVDSUB, dvdsub);
+ REGISTER_DECODER(HDMV_TEXTST, hdmv_textst);
REGISTER_DECODER(JACOSUB, jacosub);
REGISTER_DECODER(MICRODVD, microdvd);
REGISTER_ENCDEC (MOVTEXT, movtext);
new file mode 100644
@@ -0,0 +1,240 @@
+/*
+ * BluRay text subtitle decoder
+ * Copyright (c) 2014 Petri Hintukainen <phintuka@users.sourceforge.net>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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 (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/bprint.h"
+#include "libavutil/intreadwrite.h"
+#include "avcodec.h"
+#include "ass.h"
+#include "bytestream.h"
+
+enum {
+ DIALOG_STYLE_SEGMENT = 0x81,
+ DIALOG_PRESENTATION_SEGMENT = 0x82,
+};
+
+enum {
+ BD_TEXTST_DATA_ESCAPE = 0x1b,
+ BD_TEXTST_DATA_STRING = 1,
+ BD_TEXTST_DATA_FONT_ID = 2,
+ BD_TEXTST_DATA_FONT_STYLE = 3,
+ BD_TEXTST_DATA_FONT_SIZE = 4,
+ BD_TEXTST_DATA_FONT_COLOR = 5,
+ BD_TEXTST_DATA_NEWLINE = 0x0a,
+ BD_TEXTST_DATA_RESET_STYLE = 0x0b,
+};
+
+
+static void decode_region_data(AVCodecContext *avctx, GetByteContext *gb, AVBPrint *sub)
+{
+
+ while (bytestream2_get_bytes_left(gb) > 2) {
+
+ unsigned int code, type, length;
+
+ /* parse header */
+
+ code = bytestream2_get_byte(gb);
+ if (code != BD_TEXTST_DATA_ESCAPE) {
+ continue;
+ }
+ type = bytestream2_get_byte(gb);
+ length = bytestream2_get_byte(gb);
+
+ /* parse content */
+
+ if (length > bytestream2_get_bytes_left(gb)) {
+ av_log(avctx, AV_LOG_WARNING, "decode_dialog_region(): unexpected end of data\n");
+ return;
+ }
+
+ switch (type) {
+ case BD_TEXTST_DATA_STRING:
+ av_bprint_append_data(sub, gb->buffer, length);
+ break;
+ case BD_TEXTST_DATA_NEWLINE:
+ av_bprint_append_data(sub, "\\N", 2);
+ break;
+ default:
+ break;
+ }
+
+ bytestream2_skip(gb, length);
+ }
+}
+
+static int decode_region(AVCodecContext *avctx, GetByteContext *gb, AVBPrint *sub, int *forced_on_flag)
+{
+ GetByteContext gb_region;
+ int flags, data_length;
+
+ flags = bytestream2_get_byte(gb);
+ /*continous_present_flag = !!(flags & 0x80);*/
+ *forced_on_flag = !!(flags & 0x40);
+ /*region_style_id_ref=*/ bytestream2_get_byte(gb);
+ data_length = bytestream2_get_be16(gb);
+
+ if (data_length > bytestream2_get_bytes_left(gb)) {
+ av_log(avctx, AV_LOG_WARNING, "decode_dialog_region(): unexpected end of data\n");
+ return -1;
+ }
+
+ bytestream2_init(&gb_region, gb->buffer, data_length);
+ decode_region_data(avctx, &gb_region, sub);
+
+ bytestream2_skip(gb, data_length);
+
+ av_bprintf(sub, "\r\n");
+
+ return 1;
+}
+
+static int64_t decode_pts(GetByteContext *gb)
+{
+ return (((uint64_t)bytestream2_get_byte(gb) & 1) << 32) | bytestream2_get_be32(gb);
+}
+
+static int decode_palette(AVCodecContext *avctx, GetByteContext *gb)
+{
+ unsigned int length;
+
+ length = bytestream2_get_be16(gb);
+
+ if (length > bytestream2_get_bytes_left(gb)) {
+ av_log(avctx, AV_LOG_WARNING, "decode_palette(): unexpected end of data\n");
+ return -1;
+ }
+
+ /* skip palette update */
+ av_log(avctx, AV_LOG_WARNING, "ignoring palette update\n");
+ bytestream2_skip(gb, length);
+
+ return 1;
+}
+
+static void decode_presentation_segment(AVCodecContext *avctx, GetByteContext *gb, AVSubtitle *sub)
+{
+ unsigned ii, palette_update_flag, region_count;
+ int64_t start_pts, end_pts;
+ int64_t ts_start, ts_end;
+
+ start_pts = decode_pts(gb);
+ end_pts = decode_pts(gb);
+ sub->pts = start_pts;
+
+ ts_start = av_rescale_q(start_pts,
+ avctx->time_base,
+ (AVRational){1,100});
+ ts_end = av_rescale_q(end_pts,
+ avctx->time_base,
+ (AVRational){1,100});
+
+ palette_update_flag = bytestream2_get_byte(gb) >> 7;
+ if (palette_update_flag) {
+ if (decode_palette(avctx, gb) < 0) {
+ return;
+ }
+ }
+
+ region_count = bytestream2_get_byte(gb);
+ if (region_count > 2) {
+ av_log(avctx, AV_LOG_WARNING, "too many regions (%d)\n", region_count);
+ return;
+ }
+
+ for (ii = 0; ii < region_count; ii++) {
+ AVBPrint buffer;
+ char *dec_sub;
+ int forced_on_flag;
+
+ av_bprint_init(&buffer, 1024, 1024);
+
+ if (decode_region(avctx, gb, &buffer, &forced_on_flag) < 0) {
+ av_bprint_finalize(&buffer, NULL);
+ return;
+ }
+
+ av_bprint_finalize(&buffer, &dec_sub);
+
+ ff_ass_add_rect(sub, dec_sub, ts_start, ts_end - ts_start, 0);
+ av_free(dec_sub);
+
+ if (forced_on_flag && sub->num_rects > 0) {
+ sub->rects[sub->num_rects - 1]->flags |= AV_SUBTITLE_FLAG_FORCED;
+ }
+ }
+
+ if (bytestream2_get_bytes_left(gb)) {
+ av_log(avctx, AV_LOG_WARNING, "unknown data after dialog segment (%d bytes)\n", bytestream2_get_bytes_left(gb));
+ }
+}
+
+static int hdmv_decode_frame(AVCodecContext *avctx,
+ void *data, int *got_sub_ptr, AVPacket *avpkt)
+{
+ AVSubtitle *sub = data;
+ int segment_type, segment_size;
+ GetByteContext gb;
+
+ if (avpkt->size < 3) {
+ return avpkt->size;
+ }
+
+ bytestream2_init(&gb, avpkt->data, avpkt->size);
+
+ segment_type = bytestream2_get_byte(&gb);
+ segment_size = bytestream2_get_be16(&gb);
+
+ if (avpkt->size < segment_size + 3) {
+ av_log(avctx, AV_LOG_WARNING, "segment 0x%02x size mismatch: segment %d bytes, packet %d bytes\n",
+ segment_type, segment_size, avpkt->size);
+ return avpkt->size;
+ }
+
+ switch (segment_type) {
+ case DIALOG_STYLE_SEGMENT:
+ break;
+
+ case DIALOG_PRESENTATION_SEGMENT:
+ decode_presentation_segment(avctx, &gb, sub);
+ break;
+ default:
+ av_log(avctx, AV_LOG_WARNING, "unknown segment type 0x%02x\n", segment_type);
+ break;
+ }
+
+ *got_sub_ptr = (sub->num_rects > 0);
+
+ return avpkt->size;
+}
+
+static int init_codec(AVCodecContext *avctx)
+{
+ return ff_ass_subtitle_header_default(avctx);
+}
+
+AVCodec ff_hdmv_textst_decoder = {
+ .name = "hdmv_textst",
+ .long_name = NULL_IF_CONFIG_SMALL("HDMV Text subtitle"),
+ .type = AVMEDIA_TYPE_SUBTITLE,
+ .id = AV_CODEC_ID_HDMV_TEXT_SUBTITLE,
+ .init = init_codec,
+ .decode = hdmv_decode_frame,
+};