]> git.sesse.net Git - ffmpeg/blob - libavcodec/dnxhd_parser.c
Merge commit '5d8bea3bb2357bb304f8f771a4107039037c5549'
[ffmpeg] / libavcodec / dnxhd_parser.c
1 /*
2  * DNxHD/VC-3 parser
3  * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * DNxHD/VC-3 parser
25  */
26
27 #include "parser.h"
28
29 #define DNXHD_HEADER_PREFIX 0x000002800100
30
31 typedef struct {
32     ParseContext pc;
33     int interlaced;
34     int cur_field; /* first field is 0, second is 1 */
35 } DNXHDParserContext;
36
37 static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
38                                 const uint8_t *buf, int buf_size)
39 {
40     ParseContext *pc = &dctx->pc;
41     uint64_t state = pc->state64;
42     int pic_found = pc->frame_start_found;
43     int i = 0;
44     int interlaced = dctx->interlaced;
45     int cur_field = dctx->cur_field;
46
47     if (!pic_found) {
48         for (i = 0; i < buf_size; i++) {
49             state = (state << 8) | buf[i];
50             if ((state & 0xffffffffff00LL) == DNXHD_HEADER_PREFIX) {
51                 i++;
52                 pic_found = 1;
53                 interlaced = (state&2)>>1; /* byte following the 5-byte header prefix */
54                 cur_field = state&1;
55                 break;
56             }
57         }
58     }
59
60     if (pic_found) {
61         if (!buf_size) /* EOF considered as end of frame */
62             return 0;
63         for (; i < buf_size; i++) {
64             state = (state << 8) | buf[i];
65             if ((state & 0xffffffffff00LL) == DNXHD_HEADER_PREFIX) {
66                 if (!interlaced || dctx->cur_field) {
67                     pc->frame_start_found = 0;
68                     pc->state64 = -1;
69                     dctx->interlaced = interlaced;
70                     dctx->cur_field = 0;
71                     return i - 5;
72                 } else {
73                     /* continue, to get the second field */
74                     dctx->interlaced = interlaced = (state&2)>>1;
75                     dctx->cur_field = cur_field = state&1;
76                 }
77             }
78         }
79     }
80     pc->frame_start_found = pic_found;
81     pc->state64 = state;
82     dctx->interlaced = interlaced;
83     dctx->cur_field = cur_field;
84     return END_NOT_FOUND;
85 }
86
87 static int dnxhd_parse(AVCodecParserContext *s,
88                        AVCodecContext *avctx,
89                        const uint8_t **poutbuf, int *poutbuf_size,
90                        const uint8_t *buf, int buf_size)
91 {
92     DNXHDParserContext *dctx = s->priv_data;
93     ParseContext *pc = &dctx->pc;
94     int next;
95
96     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
97         next = buf_size;
98     } else {
99         next = dnxhd_find_frame_end(dctx, buf, buf_size);
100         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
101             *poutbuf      = NULL;
102             *poutbuf_size = 0;
103             return buf_size;
104         }
105     }
106     *poutbuf      = buf;
107     *poutbuf_size = buf_size;
108     return next;
109 }
110
111 AVCodecParser ff_dnxhd_parser = {
112     .codec_ids      = { AV_CODEC_ID_DNXHD },
113     .priv_data_size = sizeof(DNXHDParserContext),
114     .parser_parse   = dnxhd_parse,
115     .parser_close   = ff_parse_close,
116 };