• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/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 #include "parser.h"
00026 #include "dca.h"
00027 
00028 typedef struct DCAParseContext {
00029     ParseContext pc;
00030     uint32_t lastmarker;
00031     int size;
00032     int framesize;
00033     int hd_pos;
00034 } DCAParseContext;
00035 
00036 #define IS_MARKER(state, i, buf, buf_size) \
00037  ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
00038  || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
00039  || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE || state == DCA_HD_MARKER)
00040 
00045 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
00046                               int buf_size)
00047 {
00048     int start_found, i;
00049     uint32_t state;
00050     ParseContext *pc = &pc1->pc;
00051 
00052     start_found = pc->frame_start_found;
00053     state = pc->state;
00054 
00055     i = 0;
00056     if (!start_found) {
00057         for (i = 0; i < buf_size; i++) {
00058             state = (state << 8) | buf[i];
00059             if (IS_MARKER(state, i, buf, buf_size)) {
00060                 if (!pc1->lastmarker || state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER) {
00061                     start_found = 1;
00062                     pc1->lastmarker = state;
00063                     break;
00064                 }
00065             }
00066         }
00067     }
00068     if (start_found) {
00069         for (; i < buf_size; i++) {
00070             pc1->size++;
00071             state = (state << 8) | buf[i];
00072             if (state == DCA_HD_MARKER && !pc1->hd_pos)
00073                 pc1->hd_pos = pc1->size;
00074             if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
00075                 if(pc1->framesize > pc1->size)
00076                     continue;
00077                 // We have to check that we really read a full frame here, and that it isn't a pure HD frame, because their size is not constant.
00078                 if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){
00079                     pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
00080                 }
00081                 pc->frame_start_found = 0;
00082                 pc->state = -1;
00083                 pc1->size = 0;
00084                 return i - 3;
00085             }
00086         }
00087     }
00088     pc->frame_start_found = start_found;
00089     pc->state = state;
00090     return END_NOT_FOUND;
00091 }
00092 
00093 static av_cold int dca_parse_init(AVCodecParserContext * s)
00094 {
00095     DCAParseContext *pc1 = s->priv_data;
00096 
00097     pc1->lastmarker = 0;
00098     return 0;
00099 }
00100 
00101 static int dca_parse(AVCodecParserContext * s,
00102                      AVCodecContext * avctx,
00103                      const uint8_t ** poutbuf, int *poutbuf_size,
00104                      const uint8_t * buf, int buf_size)
00105 {
00106     DCAParseContext *pc1 = s->priv_data;
00107     ParseContext *pc = &pc1->pc;
00108     int next;
00109 
00110     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00111         next = buf_size;
00112     } else {
00113         next = dca_find_frame_end(pc1, buf, buf_size);
00114 
00115         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00116             *poutbuf = NULL;
00117             *poutbuf_size = 0;
00118             return buf_size;
00119         }
00120     }
00121     *poutbuf = buf;
00122     *poutbuf_size = buf_size;
00123     return next;
00124 }
00125 
00126 AVCodecParser ff_dca_parser = {
00127     .codec_ids      = { CODEC_ID_DTS },
00128     .priv_data_size = sizeof(DCAParseContext),
00129     .parser_init    = dca_parse_init,
00130     .parser_parse   = dca_parse,
00131     .parser_close   = ff_parse_close,
00132 };
Generated on Fri Feb 1 2013 14:34:31 for FFmpeg by doxygen 1.7.1