00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "parser.h"
00024 #include "mpegvideo.h"
00025
00026 struct MpvParseContext {
00027 ParseContext pc;
00028 AVRational frame_rate;
00029 int progressive_sequence;
00030 int width, height;
00031 };
00032
00033
00034 static void mpegvideo_extract_headers(AVCodecParserContext *s,
00035 AVCodecContext *avctx,
00036 const uint8_t *buf, int buf_size)
00037 {
00038 struct MpvParseContext *pc = s->priv_data;
00039 const uint8_t *buf_end = buf + buf_size;
00040 uint32_t start_code;
00041 int frame_rate_index, ext_type, bytes_left;
00042 int frame_rate_ext_n, frame_rate_ext_d;
00043 int top_field_first, repeat_first_field, progressive_frame;
00044 int horiz_size_ext, vert_size_ext, bit_rate_ext;
00045 int did_set_size=0;
00046 int bit_rate = 0;
00047 int vbv_delay = 0;
00048
00049 s->repeat_pict = 0;
00050
00051 while (buf < buf_end) {
00052 start_code= -1;
00053 buf= avpriv_mpv_find_start_code(buf, buf_end, &start_code);
00054 bytes_left = buf_end - buf;
00055 switch(start_code) {
00056 case PICTURE_START_CODE:
00057 if (bytes_left >= 2) {
00058 s->pict_type = (buf[1] >> 3) & 7;
00059 if (bytes_left >= 4)
00060 vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3);
00061 }
00062 break;
00063 case SEQ_START_CODE:
00064 if (bytes_left >= 7) {
00065 pc->width = (buf[0] << 4) | (buf[1] >> 4);
00066 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
00067 if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
00068 avcodec_set_dimensions(avctx, pc->width, pc->height);
00069 did_set_size=1;
00070 }
00071 frame_rate_index = buf[3] & 0xf;
00072 pc->frame_rate.den = avctx->time_base.den = ff_mpeg12_frame_rate_tab[frame_rate_index].num;
00073 pc->frame_rate.num = avctx->time_base.num = ff_mpeg12_frame_rate_tab[frame_rate_index].den;
00074 bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6);
00075 avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
00076 }
00077 break;
00078 case EXT_START_CODE:
00079 if (bytes_left >= 1) {
00080 ext_type = (buf[0] >> 4);
00081 switch(ext_type) {
00082 case 0x1:
00083 if (bytes_left >= 6) {
00084 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
00085 vert_size_ext = (buf[2] >> 5) & 3;
00086 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
00087 frame_rate_ext_n = (buf[5] >> 5) & 3;
00088 frame_rate_ext_d = (buf[5] & 0x1f);
00089 pc->progressive_sequence = buf[1] & (1 << 3);
00090 avctx->has_b_frames= !(buf[5] >> 7);
00091
00092 pc->width |=(horiz_size_ext << 12);
00093 pc->height |=( vert_size_ext << 12);
00094 bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18);
00095 if(did_set_size)
00096 avcodec_set_dimensions(avctx, pc->width, pc->height);
00097 avctx->time_base.den = pc->frame_rate.den * (frame_rate_ext_n + 1) * 2;
00098 avctx->time_base.num = pc->frame_rate.num * (frame_rate_ext_d + 1);
00099 avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
00100 }
00101 break;
00102 case 0x8:
00103 if (bytes_left >= 5) {
00104 top_field_first = buf[3] & (1 << 7);
00105 repeat_first_field = buf[3] & (1 << 1);
00106 progressive_frame = buf[4] & (1 << 7);
00107
00108
00109 s->repeat_pict = 1;
00110 if (repeat_first_field) {
00111 if (pc->progressive_sequence) {
00112 if (top_field_first)
00113 s->repeat_pict = 5;
00114 else
00115 s->repeat_pict = 3;
00116 } else if (progressive_frame) {
00117 s->repeat_pict = 2;
00118 }
00119 }
00120 }
00121 break;
00122 }
00123 }
00124 break;
00125 case -1:
00126 goto the_end;
00127 default:
00128
00129
00130 if (start_code >= SLICE_MIN_START_CODE &&
00131 start_code <= SLICE_MAX_START_CODE)
00132 goto the_end;
00133 break;
00134 }
00135 }
00136 the_end: ;
00137 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate) {
00138 avctx->rc_max_rate = 400*bit_rate;
00139 } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate &&
00140 (bit_rate != 0x3FFFF || vbv_delay != 0xFFFF)) {
00141 avctx->bit_rate = 400*bit_rate;
00142 }
00143 }
00144
00145 static int mpegvideo_parse(AVCodecParserContext *s,
00146 AVCodecContext *avctx,
00147 const uint8_t **poutbuf, int *poutbuf_size,
00148 const uint8_t *buf, int buf_size)
00149 {
00150 struct MpvParseContext *pc1 = s->priv_data;
00151 ParseContext *pc= &pc1->pc;
00152 int next;
00153
00154 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00155 next= buf_size;
00156 }else{
00157 next= ff_mpeg1_find_frame_end(pc, buf, buf_size, s);
00158
00159 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00160 *poutbuf = NULL;
00161 *poutbuf_size = 0;
00162 return buf_size;
00163 }
00164
00165 }
00166
00167
00168
00169 mpegvideo_extract_headers(s, avctx, buf, buf_size);
00170 av_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
00171 s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
00172
00173 *poutbuf = buf;
00174 *poutbuf_size = buf_size;
00175 return next;
00176 }
00177
00178 static int mpegvideo_split(AVCodecContext *avctx,
00179 const uint8_t *buf, int buf_size)
00180 {
00181 int i;
00182 uint32_t state= -1;
00183 int found=0;
00184
00185 for(i=0; i<buf_size; i++){
00186 state= (state<<8) | buf[i];
00187 if(state == 0x1B3){
00188 found=1;
00189 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
00190 return i-3;
00191 }
00192 return 0;
00193 }
00194
00195 static int mpegvideo_parse_init(AVCodecParserContext *s)
00196 {
00197 s->pict_type = AV_PICTURE_TYPE_NONE;
00198 return 0;
00199 }
00200
00201 AVCodecParser ff_mpegvideo_parser = {
00202 .codec_ids = { AV_CODEC_ID_MPEG1VIDEO, AV_CODEC_ID_MPEG2VIDEO },
00203 .priv_data_size = sizeof(struct MpvParseContext),
00204 .parser_init = mpegvideo_parse_init,
00205 .parser_parse = mpegvideo_parse,
00206 .parser_close = ff_parse_close,
00207 .split = mpegvideo_split,
00208 };