Rename var: val -> energy
[FFMpeg-mirror/DVCPRO-HD.git] / libavcodec / dca_parser.c
blobf182506f8a548d6d9b8983e602a4407dcfde4d48
1 /*
2 * DCA parser
3 * Copyright (C) 2004 Gildas Bazin
4 * Copyright (C) 2004 Benjamin Zores
5 * Copyright (C) 2006 Benjamin Larsson
6 * Copyright (C) 2007 Konstantin Shishkov
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 /**
26 * @file dca_parser.c
29 #include "parser.h"
30 #include "dca.h"
32 typedef struct DCAParseContext {
33 ParseContext pc;
34 uint32_t lastmarker;
35 int size;
36 int framesize;
37 } DCAParseContext;
39 #define IS_MARKER(state, i, buf, buf_size) \
40 ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
41 || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
42 || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
44 /**
45 * finds the end of the current frame in the bitstream.
46 * @return the position of the first byte of the next frame, or -1
48 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
49 int buf_size)
51 int start_found, i;
52 uint32_t state;
53 ParseContext *pc = &pc1->pc;
55 start_found = pc->frame_start_found;
56 state = pc->state;
58 i = 0;
59 if (!start_found) {
60 for (i = 0; i < buf_size; i++) {
61 state = (state << 8) | buf[i];
62 if (IS_MARKER(state, i, buf, buf_size)) {
63 if (pc1->lastmarker && state == pc1->lastmarker) {
64 start_found = 1;
65 break;
66 } else if (!pc1->lastmarker) {
67 start_found = 1;
68 pc1->lastmarker = state;
69 break;
74 if (start_found) {
75 for (; i < buf_size; i++) {
76 pc1->size++;
77 state = (state << 8) | buf[i];
78 if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size) && (!pc1->framesize || pc1->framesize == pc1->size)) {
79 pc->frame_start_found = 0;
80 pc->state = -1;
81 pc1->framesize = pc1->size;
82 pc1->size = 0;
83 return i - 3;
87 pc->frame_start_found = start_found;
88 pc->state = state;
89 return END_NOT_FOUND;
92 static av_cold int dca_parse_init(AVCodecParserContext * s)
94 DCAParseContext *pc1 = s->priv_data;
96 pc1->lastmarker = 0;
97 return 0;
100 static int dca_parse(AVCodecParserContext * s,
101 AVCodecContext * avctx,
102 const uint8_t ** poutbuf, int *poutbuf_size,
103 const uint8_t * buf, int buf_size)
105 DCAParseContext *pc1 = s->priv_data;
106 ParseContext *pc = &pc1->pc;
107 int next;
109 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
110 next = buf_size;
111 } else {
112 next = dca_find_frame_end(pc1, buf, buf_size);
114 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
115 *poutbuf = NULL;
116 *poutbuf_size = 0;
117 return buf_size;
120 *poutbuf = buf;
121 *poutbuf_size = buf_size;
122 return next;
125 AVCodecParser dca_parser = {
126 {CODEC_ID_DTS},
127 sizeof(DCAParseContext),
128 dca_parse_init,
129 dca_parse,
130 ff_parse_close,