Originally committed as revision 8881 to svn://svn.ffmpeg.org/ffmpeg/trunk
Showing the changes to libavcodec/dca_parser.c (1 of 4 files). Show all
Unified · Split
| @@ -0,0 +1,127 @@ | |||
| 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 | /** | ||
| 26 | * @file dca_parser.c | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "avcodec.h" | ||
| 30 | #include "parser.h" | ||
| 31 | #include "dca.h" | ||
| 32 | |||
| 33 | typedef struct DCAParseContext { | ||
| 34 | ParseContext pc; | ||
| 35 | uint32_t lastmarker; | ||
| 36 | } DCAParseContext; | ||
| 37 | |||
| 38 | #define IS_MARKER(state, i, buf, buf_size) \ | ||
| 39 | ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \ | ||
| 40 | || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \ | ||
| 41 | || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE) | ||
| 42 | |||
| 43 | /** | ||
| 44 | * finds the end of the current frame in the bitstream. | ||
| 45 | * @return the position of the first byte of the next frame, or -1 | ||
| 46 | */ | ||
| 47 | static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf, | ||
| 48 | int buf_size) | ||
| 49 | { | ||
| 50 | int start_found, i; | ||
| 51 | uint32_t state; | ||
| 52 | ParseContext *pc = &pc1->pc; | ||
| 53 | |||
| 54 | start_found = pc->frame_start_found; | ||
| 55 | state = pc->state; | ||
| 56 | |||
| 57 | i = 0; | ||
| 58 | if (!start_found) { | ||
| 59 | for (i = 0; i < buf_size; i++) { | ||
| 60 | state = (state << 8) | buf[i]; | ||
| 61 | if (IS_MARKER(state, i, buf, buf_size)) { | ||
| 62 | if (pc1->lastmarker && state == pc1->lastmarker) { | ||
| 63 | start_found = 1; | ||
| 64 | break; | ||
| 65 | } else if (!pc1->lastmarker) { | ||
| 66 | start_found = 1; | ||
| 67 | pc1->lastmarker = state; | ||
| 68 | break; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | if (start_found) { | ||
| 74 | for (; i < buf_size; i++) { | ||
| 75 | state = (state << 8) | buf[i]; | ||
| 76 | if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) { | ||
| 77 | pc->frame_start_found = 0; | ||
| 78 | pc->state = -1; | ||
| 79 | return i - 3; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | pc->frame_start_found = start_found; | ||
| 84 | pc->state = state; | ||
| 85 | return END_NOT_FOUND; | ||
| 86 | } | ||
| 87 | |||
| 88 | static int dca_parse_init(AVCodecParserContext * s) | ||
| 89 | { | ||
| 90 | DCAParseContext *pc1 = s->priv_data; | ||
| 91 | |||
| 92 | pc1->lastmarker = 0; | ||
| 93 | return 0; | ||
| 94 | } | ||
| 95 | |||
| 96 | static int dca_parse(AVCodecParserContext * s, | ||
| 97 | AVCodecContext * avctx, | ||
| 98 | uint8_t ** poutbuf, int *poutbuf_size, | ||
| 99 | const uint8_t * buf, int buf_size) | ||
| 100 | { | ||
| 101 | DCAParseContext *pc1 = s->priv_data; | ||
| 102 | ParseContext *pc = &pc1->pc; | ||
| 103 | int next; | ||
| 104 | |||
| 105 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { | ||
| 106 | next = buf_size; | ||
| 107 | } else { | ||
| 108 | next = dca_find_frame_end(pc1, buf, buf_size); | ||
| 109 | |||
| 110 | if (ff_combine_frame(pc, next, (uint8_t **) & buf, &buf_size) < 0) { | ||
| 111 | *poutbuf = NULL; | ||
| 112 | *poutbuf_size = 0; | ||
| 113 | return buf_size; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | *poutbuf = (uint8_t *) buf; | ||
| 117 | *poutbuf_size = buf_size; | ||
| 118 | return next; | ||
| 119 | } | ||
| 120 | |||
| 121 | AVCodecParser dca_parser = { | ||
| 122 | {CODEC_ID_DTS}, | ||
| 123 | sizeof(DCAParseContext), | ||
| 124 | dca_parse_init, | ||
| 125 | dca_parse, | ||
| 126 | ff_parse_close, | ||
| 127 | }; | ||