dca_parser.c

Go to the documentation of this file.
00001 /*
00002  * DCA parser
00003  * Copyright (C) 2004 Gildas Bazin
00004  * Copyright (C) 2004 Benjamin Zores
00005  * Copyright (C) 2006 Benjamin Larsson
00006  * Copyright (C) 2007 Konstantin Shishkov
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00025 /**
00026  * @file dca_parser.c
00027  */
00028 
00029 #include "parser.h"
00030 #include "dca.h"
00031 
00032 typedef struct DCAParseContext {
00033     ParseContext pc;
00034     uint32_t lastmarker;
00035     int size;
00036     int framesize;
00037 } DCAParseContext;
00038 
00039 #define IS_MARKER(state, i, buf, buf_size) \
00040  ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
00041  || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
00042  || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
00043 
00044 /**
00045  * finds the end of the current frame in the bitstream.
00046  * @return the position of the first byte of the next frame, or -1
00047  */
00048 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
00049                               int buf_size)
00050 {
00051     int start_found, i;
00052     uint32_t state;
00053     ParseContext *pc = &pc1->pc;
00054 
00055     start_found = pc->frame_start_found;
00056     state = pc->state;
00057 
00058     i = 0;
00059     if (!start_found) {
00060         for (i = 0; i < buf_size; i++) {
00061             state = (state << 8) | buf[i];
00062             if (IS_MARKER(state, i, buf, buf_size)) {
00063                 if (pc1->lastmarker && state == pc1->lastmarker) {
00064                     start_found = 1;
00065                     break;
00066                 } else if (!pc1->lastmarker) {
00067                     start_found = 1;
00068                     pc1->lastmarker = state;
00069                     break;
00070                 }
00071             }
00072         }
00073     }
00074     if (start_found) {
00075         for (; i < buf_size; i++) {
00076             pc1->size++;
00077             state = (state << 8) | buf[i];
00078             if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size) && (!pc1->framesize || pc1->framesize == pc1->size)) {
00079                 pc->frame_start_found = 0;
00080                 pc->state = -1;
00081                 pc1->framesize = pc1->size;
00082                 pc1->size = 0;
00083                 return i - 3;
00084             }
00085         }
00086     }
00087     pc->frame_start_found = start_found;
00088     pc->state = state;
00089     return END_NOT_FOUND;
00090 }
00091 
00092 static av_cold int dca_parse_init(AVCodecParserContext * s)
00093 {
00094     DCAParseContext *pc1 = s->priv_data;
00095 
00096     pc1->lastmarker = 0;
00097     return 0;
00098 }
00099 
00100 static int dca_parse(AVCodecParserContext * s,
00101                      AVCodecContext * avctx,
00102                      const uint8_t ** poutbuf, int *poutbuf_size,
00103                      const uint8_t * buf, int buf_size)
00104 {
00105     DCAParseContext *pc1 = s->priv_data;
00106     ParseContext *pc = &pc1->pc;
00107     int next;
00108 
00109     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00110         next = buf_size;
00111     } else {
00112         next = dca_find_frame_end(pc1, buf, buf_size);
00113 
00114         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00115             *poutbuf = NULL;
00116             *poutbuf_size = 0;
00117             return buf_size;
00118         }
00119     }
00120     *poutbuf = buf;
00121     *poutbuf_size = buf_size;
00122     return next;
00123 }
00124 
00125 AVCodecParser dca_parser = {
00126     {CODEC_ID_DTS},
00127     sizeof(DCAParseContext),
00128     dca_parse_init,
00129     dca_parse,
00130     ff_parse_close,
00131 };

Generated on Fri Jan 9 14:44:25 2009 for libextractor by  doxygen 1.5.1