00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "dsputil.h"
00023 #include "bitstream.h"
00024
00025
00026
00027
00028
00029
00030 #define PARSE_BUF_SIZE (65536)
00031
00032
00033
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 #ifdef DEBUG
00059 av_log(avctx, AV_LOG_INFO, "DVB parse packet pts=%"PRIx64", lpts=%"PRIx64", cpts=%"PRIx64":\n",
00060 s->pts, s->last_pts, s->cur_frame_pts[s->cur_frame_start_index]);
00061 #endif
00062
00063 #ifdef DEBUG_PACKET_CONTENTS
00064 int i;
00065
00066 for (i=0; i < buf_size; i++)
00067 {
00068 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
00069 if (i % 16 == 15)
00070 av_log(avctx, AV_LOG_INFO, "\n");
00071 }
00072
00073 if (i % 16 != 0)
00074 av_log(avctx, AV_LOG_INFO, "\n");
00075
00076 #endif
00077
00078 *poutbuf = NULL;
00079 *poutbuf_size = 0;
00080
00081 s->fetch_timestamp = 1;
00082
00083 if (s->last_pts != s->pts && s->pts != AV_NOPTS_VALUE)
00084 {
00085 if (pc->packet_index != pc->packet_start)
00086 {
00087 #ifdef DEBUG
00088 av_log(avctx, AV_LOG_INFO, "Discarding %d bytes\n",
00089 pc->packet_index - pc->packet_start);
00090 #endif
00091 }
00092
00093 pc->packet_start = 0;
00094 pc->packet_index = 0;
00095
00096 if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) {
00097 #ifdef DEBUG
00098 av_log(avctx, AV_LOG_INFO, "Bad packet header\n");
00099 #endif
00100 return -1;
00101 }
00102
00103 buf_pos = 2;
00104
00105 pc->in_packet = 1;
00106 } else {
00107 if (pc->packet_start != 0)
00108 {
00109 if (pc->packet_index != pc->packet_start)
00110 {
00111 memmove(pc->packet_buf, pc->packet_buf + pc->packet_start,
00112 pc->packet_index - pc->packet_start);
00113
00114 pc->packet_index -= pc->packet_start;
00115 pc->packet_start = 0;
00116 } else {
00117 pc->packet_start = 0;
00118 pc->packet_index = 0;
00119 }
00120 }
00121 }
00122
00123 if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE)
00124 return -1;
00125
00126
00127 if (pc->in_packet == 0)
00128 return buf_size;
00129
00130 memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos);
00131 pc->packet_index += buf_size - buf_pos;
00132
00133 p = pc->packet_buf;
00134 p_end = pc->packet_buf + pc->packet_index;
00135
00136 while (p < p_end)
00137 {
00138 if (*p == 0x0f)
00139 {
00140 if (p + 6 <= p_end)
00141 {
00142 len = AV_RB16(p + 4);
00143
00144 if (p + len + 6 <= p_end)
00145 {
00146 *poutbuf_size += len + 6;
00147
00148 p += len + 6;
00149 } else
00150 break;
00151 } else
00152 break;
00153 } else if (*p == 0xff) {
00154 if (p + 1 < p_end)
00155 {
00156 #ifdef DEBUG
00157 av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
00158 #endif
00159 }
00160 pc->packet_index = p - pc->packet_buf;
00161 pc->in_packet = 0;
00162 break;
00163 } else {
00164 av_log(avctx, AV_LOG_ERROR, "Junk in packet\n");
00165
00166 pc->packet_index = p - pc->packet_buf;
00167 pc->in_packet = 0;
00168 break;
00169 }
00170 }
00171
00172 if (*poutbuf_size > 0)
00173 {
00174 *poutbuf = pc->packet_buf;
00175 pc->packet_start = *poutbuf_size;
00176 }
00177
00178 if (s->pts == AV_NOPTS_VALUE)
00179 s->pts = s->last_pts;
00180
00181 return buf_size;
00182 }
00183
00184 static av_cold void dvbsub_parse_close(AVCodecParserContext *s)
00185 {
00186 DVBSubParseContext *pc = s->priv_data;
00187 av_freep(&pc->packet_buf);
00188 }
00189
00190 AVCodecParser dvbsub_parser = {
00191 { CODEC_ID_DVB_SUBTITLE },
00192 sizeof(DVBSubParseContext),
00193 dvbsub_parse_init,
00194 dvbsub_parse,
00195 dvbsub_parse_close,
00196 };