]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca_parser.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / dca_parser.c
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
7  *
8  * This file is part of FFmpeg.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "parser.h"
26 #include "dca.h"
27
28 typedef struct DCAParseContext {
29     ParseContext pc;
30     uint32_t lastmarker;
31     int size;
32     int framesize;
33     int hd_pos;
34 } DCAParseContext;
35
36 #define IS_MARKER(state, i, buf, buf_size) \
37  ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
38  || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
39  || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE || state == DCA_HD_MARKER)
40
41 /**
42  * finds the end of the current frame in the bitstream.
43  * @return the position of the first byte of the next frame, or -1
44  */
45 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
46                               int buf_size)
47 {
48     int start_found, i;
49     uint32_t state;
50     ParseContext *pc = &pc1->pc;
51
52     start_found = pc->frame_start_found;
53     state = pc->state;
54
55     i = 0;
56     if (!start_found) {
57         for (i = 0; i < buf_size; i++) {
58             state = (state << 8) | buf[i];
59             if (IS_MARKER(state, i, buf, buf_size)) {
60                 if (!pc1->lastmarker || state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER) {
61                     start_found = 1;
62                     pc1->lastmarker = state;
63                     break;
64                 }
65             }
66         }
67     }
68     if (start_found) {
69         for (; i < buf_size; i++) {
70             pc1->size++;
71             state = (state << 8) | buf[i];
72             if (state == DCA_HD_MARKER && !pc1->hd_pos)
73                 pc1->hd_pos = pc1->size;
74             if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
75                 if(pc1->framesize > pc1->size)
76                     continue;
77                 // 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.
78                 if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){
79                     pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
80                 }
81                 pc->frame_start_found = 0;
82                 pc->state = -1;
83                 pc1->size = 0;
84                 return i - 3;
85             }
86         }
87     }
88     pc->frame_start_found = start_found;
89     pc->state = state;
90     return END_NOT_FOUND;
91 }
92
93 static av_cold int dca_parse_init(AVCodecParserContext * s)
94 {
95     DCAParseContext *pc1 = s->priv_data;
96
97     pc1->lastmarker = 0;
98     return 0;
99 }
100
101 static int dca_parse(AVCodecParserContext * s,
102                      AVCodecContext * avctx,
103                      const uint8_t ** poutbuf, int *poutbuf_size,
104                      const uint8_t * buf, int buf_size)
105 {
106     DCAParseContext *pc1 = s->priv_data;
107     ParseContext *pc = &pc1->pc;
108     int next;
109
110     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
111         next = buf_size;
112     } else {
113         next = dca_find_frame_end(pc1, buf, buf_size);
114
115         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
116             *poutbuf = NULL;
117             *poutbuf_size = 0;
118             return buf_size;
119         }
120     }
121     *poutbuf = buf;
122     *poutbuf_size = buf_size;
123     return next;
124 }
125
126 AVCodecParser ff_dca_parser = {
127     {CODEC_ID_DTS},
128     sizeof(DCAParseContext),
129     dca_parse_init,
130     dca_parse,
131     ff_parse_close,
132 };