libavcodec/opus/parse.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Opus decoder/parser common functions and structures | ||
| 3 | * Copyright (c) 2012 Andrew D'Addesio | ||
| 4 | * Copyright (c) 2013-2014 Mozilla Corporation | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifndef AVCODEC_OPUS_PARSE_H | ||
| 24 | #define AVCODEC_OPUS_PARSE_H | ||
| 25 | |||
| 26 | #include <stdint.h> | ||
| 27 | |||
| 28 | #include "libavcodec/avcodec.h" | ||
| 29 | #include "opus.h" | ||
| 30 | #include "tab.h" | ||
| 31 | |||
| 32 | typedef struct OpusPacket { | ||
| 33 | int packet_size; /**< packet size */ | ||
| 34 | int data_size; /**< size of the useful data -- packet size - padding */ | ||
| 35 | int code; /**< packet code: specifies the frame layout */ | ||
| 36 | int stereo; /**< whether this packet is mono or stereo */ | ||
| 37 | int vbr; /**< vbr flag */ | ||
| 38 | int config; /**< configuration: tells the audio mode, | ||
| 39 | ** bandwidth, and frame duration */ | ||
| 40 | int frame_count; /**< frame count */ | ||
| 41 | int frame_offset[OPUS_MAX_FRAMES]; /**< frame offsets */ | ||
| 42 | int frame_size[OPUS_MAX_FRAMES]; /**< frame sizes */ | ||
| 43 | int frame_duration; /**< frame duration, in samples @ 48kHz */ | ||
| 44 | enum OpusMode mode; /**< mode */ | ||
| 45 | enum OpusBandwidth bandwidth; /**< bandwidth */ | ||
| 46 | } OpusPacket; | ||
| 47 | |||
| 48 | // a mapping between an opus stream and an output channel | ||
| 49 | typedef struct ChannelMap { | ||
| 50 | int stream_idx; | ||
| 51 | int channel_idx; | ||
| 52 | |||
| 53 | // when a single decoded channel is mapped to multiple output channels, we | ||
| 54 | // write to the first output directly and copy from it to the others | ||
| 55 | // this field is set to 1 for those copied output channels | ||
| 56 | int copy; | ||
| 57 | // this is the index of the output channel to copy from | ||
| 58 | int copy_idx; | ||
| 59 | |||
| 60 | // this channel is silent | ||
| 61 | int silence; | ||
| 62 | } ChannelMap; | ||
| 63 | |||
| 64 | typedef struct OpusParseContext { | ||
| 65 | int nb_streams; | ||
| 66 | int nb_stereo_streams; | ||
| 67 | |||
| 68 | int16_t gain_i; | ||
| 69 | |||
| 70 | ChannelMap *channel_maps; | ||
| 71 | } OpusParseContext; | ||
| 72 | |||
| 73 | int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, | ||
| 74 | int self_delimited); | ||
| 75 | |||
| 76 | int ff_opus_parse_extradata(AVCodecContext *avctx, OpusParseContext *s, | ||
| 77 | const uint8_t *header, int header_size); | ||
| 78 | |||
| 79 | /** | ||
| 80 | * @return the duration of an Opus packet in samples at OPUS_TIMESTAMP_RATE, | ||
| 81 | * read from its TOC, or AVERROR_INVALIDDATA if the packet is too | ||
| 82 | * short or longer than 120 ms | ||
| 83 | */ | ||
| 84 | 533 | static inline int ff_opus_packet_samples(const uint8_t *buf, int buf_size) | |
| 85 | { | ||
| 86 | int frame_count, samples; | ||
| 87 | |||
| 88 | 533 | if (buf_size < 1) | |
| 89 | ✗ | return AVERROR_INVALIDDATA; | |
| 90 | 533 | switch (buf[0] & 3) { | |
| 91 | 533 | case 0: | |
| 92 | 533 | frame_count = 1; | |
| 93 | 533 | break; | |
| 94 | ✗ | case 3: | |
| 95 | ✗ | if (buf_size < 2) | |
| 96 | ✗ | return AVERROR_INVALIDDATA; | |
| 97 | ✗ | frame_count = buf[1] & 0x3F; | |
| 98 | ✗ | break; | |
| 99 | ✗ | default: | |
| 100 | ✗ | frame_count = 2; | |
| 101 | ✗ | break; | |
| 102 | } | ||
| 103 | 533 | samples = frame_count * ff_opus_frame_duration[buf[0] >> 3]; | |
| 104 | // A packet holds at most 120 ms. | ||
| 105 | return samples > 120 * OPUS_TIMESTAMP_RATE / 1000 ? AVERROR_INVALIDDATA | ||
| 106 | 533 | : samples; | |
| 107 | } | ||
| 108 | |||
| 109 | #endif /* AVCODEC_OPUS_PARSE_H */ | ||
| 110 |