Libav
|
00001 /* 00002 * DVB subtitle parser for FFmpeg 00003 * Copyright (c) 2005 Ian Caulfield 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 #include "avcodec.h" 00022 #include "dsputil.h" 00023 #include "get_bits.h" 00024 00025 //#define DEBUG 00026 //#define DEBUG_PACKET_CONTENTS 00027 00028 /* Parser (mostly) copied from dvdsub.c */ 00029 00030 #define PARSE_BUF_SIZE (65536) 00031 00032 00033 /* parser definition */ 00034 typedef struct DVBSubParseContext { 00035 uint8_t *packet_buf; 00036 int packet_start; 00037 int packet_index; 00038 int in_packet; 00039 } DVBSubParseContext; 00040 00041 static av_cold int dvbsub_parse_init(AVCodecParserContext *s) 00042 { 00043 DVBSubParseContext *pc = s->priv_data; 00044 pc->packet_buf = av_malloc(PARSE_BUF_SIZE); 00045 00046 return 0; 00047 } 00048 00049 static int dvbsub_parse(AVCodecParserContext *s, 00050 AVCodecContext *avctx, 00051 const uint8_t **poutbuf, int *poutbuf_size, 00052 const uint8_t *buf, int buf_size) 00053 { 00054 DVBSubParseContext *pc = s->priv_data; 00055 uint8_t *p, *p_end; 00056 int len, buf_pos = 0; 00057 00058 dprintf(avctx, "DVB parse packet pts=%"PRIx64", lpts=%"PRIx64", cpts=%"PRIx64":\n", 00059 s->pts, s->last_pts, s->cur_frame_pts[s->cur_frame_start_index]); 00060 00061 #ifdef DEBUG_PACKET_CONTENTS 00062 int i; 00063 00064 for (i=0; i < buf_size; i++) 00065 { 00066 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]); 00067 if (i % 16 == 15) 00068 av_log(avctx, AV_LOG_INFO, "\n"); 00069 } 00070 00071 if (i % 16 != 0) 00072 av_log(avctx, AV_LOG_INFO, "\n"); 00073 00074 #endif 00075 00076 *poutbuf = NULL; 00077 *poutbuf_size = 0; 00078 00079 s->fetch_timestamp = 1; 00080 00081 if (s->last_pts != s->pts && s->pts != AV_NOPTS_VALUE) /* Start of a new packet */ 00082 { 00083 if (pc->packet_index != pc->packet_start) 00084 { 00085 dprintf(avctx, "Discarding %d bytes\n", 00086 pc->packet_index - pc->packet_start); 00087 } 00088 00089 pc->packet_start = 0; 00090 pc->packet_index = 0; 00091 00092 if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) { 00093 dprintf(avctx, "Bad packet header\n"); 00094 return -1; 00095 } 00096 00097 buf_pos = 2; 00098 00099 pc->in_packet = 1; 00100 } else { 00101 if (pc->packet_start != 0) 00102 { 00103 if (pc->packet_index != pc->packet_start) 00104 { 00105 memmove(pc->packet_buf, pc->packet_buf + pc->packet_start, 00106 pc->packet_index - pc->packet_start); 00107 00108 pc->packet_index -= pc->packet_start; 00109 pc->packet_start = 0; 00110 } else { 00111 pc->packet_start = 0; 00112 pc->packet_index = 0; 00113 } 00114 } 00115 } 00116 00117 if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE) 00118 return -1; 00119 00120 /* if not currently in a packet, discard data */ 00121 if (pc->in_packet == 0) 00122 return buf_size; 00123 00124 memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos); 00125 pc->packet_index += buf_size - buf_pos; 00126 00127 p = pc->packet_buf; 00128 p_end = pc->packet_buf + pc->packet_index; 00129 00130 while (p < p_end) 00131 { 00132 if (*p == 0x0f) 00133 { 00134 if (p + 6 <= p_end) 00135 { 00136 len = AV_RB16(p + 4); 00137 00138 if (p + len + 6 <= p_end) 00139 { 00140 *poutbuf_size += len + 6; 00141 00142 p += len + 6; 00143 } else 00144 break; 00145 } else 00146 break; 00147 } else if (*p == 0xff) { 00148 if (p + 1 < p_end) 00149 { 00150 dprintf(avctx, "Junk at end of packet\n"); 00151 } 00152 pc->packet_index = p - pc->packet_buf; 00153 pc->in_packet = 0; 00154 break; 00155 } else { 00156 av_log(avctx, AV_LOG_ERROR, "Junk in packet\n"); 00157 00158 pc->packet_index = p - pc->packet_buf; 00159 pc->in_packet = 0; 00160 break; 00161 } 00162 } 00163 00164 if (*poutbuf_size > 0) 00165 { 00166 *poutbuf = pc->packet_buf; 00167 pc->packet_start = *poutbuf_size; 00168 } 00169 00170 if (s->pts == AV_NOPTS_VALUE) 00171 s->pts = s->last_pts; 00172 00173 return buf_size; 00174 } 00175 00176 static av_cold void dvbsub_parse_close(AVCodecParserContext *s) 00177 { 00178 DVBSubParseContext *pc = s->priv_data; 00179 av_freep(&pc->packet_buf); 00180 } 00181 00182 AVCodecParser dvbsub_parser = { 00183 { CODEC_ID_DVB_SUBTITLE }, 00184 sizeof(DVBSubParseContext), 00185 dvbsub_parse_init, 00186 dvbsub_parse, 00187 dvbsub_parse_close, 00188 };