avcodec/x86/h264_idct: Fix ff_h264_luma_dc_dequant_idct_sse2 checkasm failures
[ffmpeg.git] / libavformat / bit.c
1 /*
2 * G.729 bit format muxer and demuxer
3 * Copyright (c) 2007-2008 Vladimir Voroshilov
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 #include "config_components.h"
23
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "demux.h"
27 #include "internal.h"
28 #include "mux.h"
29 #include "libavcodec/get_bits.h"
30 #include "libavcodec/put_bits.h"
31
32 #define MAX_FRAME_SIZE 10
33
34 #define SYNC_WORD 0x6b21
35 #define BIT_0 0x7f
36 #define BIT_1 0x81
37
38 #if CONFIG_BIT_DEMUXER
39 static int probe(const AVProbeData *p)
40 {
41 int i = 0, j, valid = 0;
42
43 while (2 * i + 3 < p->buf_size){
44 if (AV_RL16(&p->buf[2 * i++]) != SYNC_WORD)
45 return 0;
46 j = AV_RL16(&p->buf[2 * i++]);
47 if (j != 0 && j != 0x10 && j != 0x40 && j != 0x50 && j != 0x76)
48 return 0;
49 if (j)
50 valid++;
51 i += j;
52 }
53 if (valid > 10)
54 return AVPROBE_SCORE_MAX;
55 if (valid > 2)
56 return AVPROBE_SCORE_EXTENSION - 1;
57 return 0;
58 }
59
60 static int read_header(AVFormatContext *s)
61 {
62 AVStream* st;
63
64 st=avformat_new_stream(s, NULL);
65 if (!st)
66 return AVERROR(ENOMEM);
67
68 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
69 st->codecpar->codec_id=AV_CODEC_ID_G729;
70 st->codecpar->sample_rate=8000;
71 st->codecpar->block_align = 16;
72 st->codecpar->ch_layout.nb_channels = 1;
73
74 avpriv_set_pts_info(st, 64, 1, 100);
75 return 0;
76 }
77
78 static int read_packet(AVFormatContext *s,
79 AVPacket *pkt)
80 {
81 AVIOContext *pb = s->pb;
82 PutBitContext pbo;
83 uint16_t buf[8 * MAX_FRAME_SIZE + 2];
84 int packet_size;
85 uint16_t* src=buf;
86 int i, j, ret;
87 int64_t pos= avio_tell(pb);
88
89 if(avio_feof(pb))
90 return AVERROR_EOF;
91
92 avio_rl16(pb); // sync word
93 packet_size = avio_rl16(pb) / 8;
94 if(packet_size > MAX_FRAME_SIZE)
95 return AVERROR_INVALIDDATA;
96
97 ret = ffio_read_size(pb, (uint8_t*)buf, (8 * packet_size) * sizeof(uint16_t));
98 if(ret<0)
99 return ret;
100
101 if ((ret = av_new_packet(pkt, packet_size)) < 0)
102 return ret;
103
104 init_put_bits(&pbo, pkt->data, packet_size);
105 for(j=0; j < packet_size; j++)
106 for(i=0; i<8;i++)
107 put_bits(&pbo,1, AV_RL16(src++) == BIT_1 ? 1 : 0);
108
109 flush_put_bits(&pbo);
110
111 pkt->duration=1;
112 pkt->pos = pos;
113 return 0;
114 }
115
116 const FFInputFormat ff_bit_demuxer = {
117 .p.name = "bit",
118 .p.long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
119 .p.extensions = "bit",
120 .read_probe = probe,
121 .read_header = read_header,
122 .read_packet = read_packet,
123 };
124 #endif
125
126 #if CONFIG_BIT_MUXER
127 static av_cold int init(AVFormatContext *s)
128 {
129 AVCodecParameters *par = s->streams[0]->codecpar;
130
131 if (par->ch_layout.nb_channels != 1) {
132 av_log(s, AV_LOG_ERROR,
133 "only codec g729 with 1 channel is supported by this format\n");
134 return AVERROR(EINVAL);
135 }
136
137 par->bits_per_coded_sample = 16;
138 par->block_align = (par->bits_per_coded_sample * par->ch_layout.nb_channels) >> 3;
139
140 return 0;
141 }
142
143 static int write_packet(AVFormatContext *s, AVPacket *pkt)
144 {
145 AVIOContext *pb = s->pb;
146 GetBitContext gb;
147 int i;
148
149 if (pkt->size != 10)
150 return AVERROR(EINVAL);
151
152 avio_wl16(pb, SYNC_WORD);
153 avio_wl16(pb, 8 * pkt->size);
154
155 init_get_bits(&gb, pkt->data, 8 * pkt->size);
156 for (i = 0; i < 8 * pkt->size; i++)
157 avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
158
159 return 0;
160 }
161
162 const FFOutputFormat ff_bit_muxer = {
163 .p.name = "bit",
164 .p.long_name = NULL_IF_CONFIG_SMALL("G.729 BIT file format"),
165 .p.mime_type = "audio/bit",
166 .p.extensions = "bit",
167 .p.audio_codec = AV_CODEC_ID_G729,
168 .p.video_codec = AV_CODEC_ID_NONE,
169 .p.subtitle_codec = AV_CODEC_ID_NONE,
170 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
171 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
172 .init = init,
173 .write_packet = write_packet,
174 };
175 #endif