|
808
|
1 /*
|
|
|
2 * utils for libavcodec
|
|
|
3 * Copyright (c) 2001 Fabrice Bellard.
|
|
815
|
4 * Copyright (c) 2003 Michel Bardiaux for the av_log API
|
|
808
|
5 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
|
|
815
|
6 * Copyright (c) 2004 Roman Bogorodskiy (bmp-wma specific stuff)
|
|
808
|
7 *
|
|
815
|
8 * This library is free software; you can redistribute it and/or
|
|
808
|
9 * modify it under the terms of the GNU Lesser General Public
|
|
|
10 * License as published by the Free Software Foundation; either
|
|
815
|
11 * version 2 of the License, or (at your option) any later version.
|
|
808
|
12 *
|
|
815
|
13 * This library is distributed in the hope that it will be useful,
|
|
808
|
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
|
|
815
|
19 * License along with this library; if not, write to the Free Software
|
|
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
808
|
21 */
|
|
815
|
22
|
|
808
|
23 #include "avcodec.h"
|
|
|
24 #include "dsputil.h"
|
|
|
25 #include <stdarg.h>
|
|
|
26
|
|
|
27 /* encoder management */
|
|
815
|
28 AVCodec *first_avcodec;
|
|
808
|
29
|
|
|
30 void register_avcodec(AVCodec *format)
|
|
|
31 {
|
|
|
32 AVCodec **p;
|
|
|
33 p = &first_avcodec;
|
|
|
34 while (*p != NULL) p = &(*p)->next;
|
|
|
35 *p = format;
|
|
|
36 format->next = NULL;
|
|
|
37 }
|
|
|
38
|
|
|
39 typedef struct InternalBuffer{
|
|
|
40 int last_pic_num;
|
|
|
41 uint8_t *base[4];
|
|
|
42 uint8_t *data[4];
|
|
|
43 int linesize[4];
|
|
|
44 }InternalBuffer;
|
|
|
45
|
|
818
|
46 const uint8_t ff_reverse[256]={
|
|
|
47 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
|
|
|
48 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
|
|
|
49 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
|
|
|
50 0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
|
|
|
51 0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
|
|
|
52 0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
|
|
|
53 0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
|
|
|
54 0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
|
|
|
55 0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
|
|
|
56 0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
|
|
|
57 0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
|
|
|
58 0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
|
|
|
59 0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
|
|
|
60 0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
|
|
|
61 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
|
|
|
62 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
|
|
|
63 };
|
|
|
64
|
|
|
65
|
|
808
|
66 #define INTERNAL_BUFFER_SIZE 32
|
|
|
67
|
|
815
|
68 #undef ALIGN
|
|
808
|
69 #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
|
|
|
70
|
|
|
71 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
|
|
815
|
72 int w_align= 1;
|
|
|
73 int h_align= 1;
|
|
|
74
|
|
808
|
75 switch(s->pix_fmt){
|
|
|
76 case PIX_FMT_YUV420P:
|
|
|
77 case PIX_FMT_YUV422:
|
|
|
78 case PIX_FMT_YUV422P:
|
|
|
79 case PIX_FMT_YUV444P:
|
|
|
80 case PIX_FMT_GRAY8:
|
|
|
81 case PIX_FMT_YUVJ420P:
|
|
|
82 case PIX_FMT_YUVJ422P:
|
|
|
83 case PIX_FMT_YUVJ444P:
|
|
|
84 w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
|
|
|
85 h_align= 16;
|
|
|
86 break;
|
|
|
87 case PIX_FMT_YUV411P:
|
|
|
88 w_align=32;
|
|
|
89 h_align=8;
|
|
|
90 break;
|
|
|
91 case PIX_FMT_YUV410P:
|
|
|
92 default:
|
|
|
93 w_align= 1;
|
|
|
94 h_align= 1;
|
|
|
95 break;
|
|
|
96 }
|
|
|
97
|
|
|
98 *width = ALIGN(*width , w_align);
|
|
|
99 *height= ALIGN(*height, h_align);
|
|
|
100 }
|
|
|
101
|
|
818
|
102 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
|
|
|
103 return fmt[0];
|
|
|
104 }
|
|
|
105
|
|
808
|
106 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
|
|
|
107 int i;
|
|
|
108 InternalBuffer *buf, *last, temp;
|
|
|
109
|
|
|
110 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
|
|
|
111 assert(s->internal_buffer_count);
|
|
|
112
|
|
|
113 buf = NULL; /* avoids warning */
|
|
|
114 for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
|
|
|
115 buf= &((InternalBuffer*)s->internal_buffer)[i];
|
|
|
116 if(buf->data[0] == pic->data[0])
|
|
|
117 break;
|
|
|
118 }
|
|
|
119 assert(i < s->internal_buffer_count);
|
|
|
120 s->internal_buffer_count--;
|
|
|
121 last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
|
|
|
122
|
|
|
123 temp= *buf;
|
|
|
124 *buf= *last;
|
|
|
125 *last= temp;
|
|
|
126
|
|
|
127 for(i=0; i<3; i++){
|
|
|
128 pic->data[i]=NULL;
|
|
|
129 // pic->base[i]=NULL;
|
|
|
130 }
|
|
|
131 }
|
|
|
132
|
|
|
133
|
|
|
134 void avcodec_get_context_defaults(AVCodecContext *s){
|
|
815
|
135 s->bit_rate= 800*1000;
|
|
|
136 s->bit_rate_tolerance= s->bit_rate*10;
|
|
|
137 s->qmin= 2;
|
|
|
138 s->qmax= 31;
|
|
|
139 s->mb_qmin= 2;
|
|
|
140 s->mb_qmax= 31;
|
|
808
|
141 s->rc_eq= "tex^qComp";
|
|
815
|
142 s->qcompress= 0.5;
|
|
|
143 s->max_qdiff= 3;
|
|
|
144 s->b_quant_factor=1.25;
|
|
|
145 s->b_quant_offset=1.25;
|
|
|
146 s->i_quant_factor=-0.8;
|
|
|
147 s->i_quant_offset=0.0;
|
|
|
148 s->error_concealment= 3;
|
|
|
149 s->error_resilience= 1;
|
|
|
150 s->workaround_bugs= FF_BUG_AUTODETECT;
|
|
|
151 s->gop_size= 50;
|
|
|
152 s->me_method= ME_EPZS;
|
|
|
153 //s->get_buffer= avcodec_default_get_buffer;
|
|
808
|
154 s->release_buffer= avcodec_default_release_buffer;
|
|
|
155 s->get_format= avcodec_default_get_format;
|
|
815
|
156 s->me_subpel_quality=8;
|
|
|
157 s->lmin= FF_QP2LAMBDA * s->qmin;
|
|
|
158 s->lmax= FF_QP2LAMBDA * s->qmax;
|
|
|
159 //s->sample_aspect_ratio= (AVRational){0,1};
|
|
|
160 s->ildct_cmp= FF_CMP_VSAD;
|
|
|
161
|
|
|
162 s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
|
|
|
163 s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
|
|
808
|
164 s->palctrl = NULL;
|
|
815
|
165 //s->reget_buffer= avcodec_default_reget_buffer;
|
|
808
|
166 }
|
|
|
167
|
|
|
168 /**
|
|
|
169 * allocates a AVCodecContext and set it to defaults.
|
|
815
|
170 * this can be deallocated by simply calling free()
|
|
808
|
171 */
|
|
|
172 AVCodecContext *avcodec_alloc_context(void){
|
|
815
|
173 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
|
|
|
174
|
|
808
|
175 if(avctx==NULL) return NULL;
|
|
815
|
176
|
|
808
|
177 avcodec_get_context_defaults(avctx);
|
|
815
|
178
|
|
808
|
179 return avctx;
|
|
|
180 }
|
|
|
181
|
|
|
182 /**
|
|
|
183 * allocates a AVPFrame and set it to defaults.
|
|
815
|
184 * this can be deallocated by simply calling free()
|
|
808
|
185 */
|
|
|
186 AVFrame *avcodec_alloc_frame(void){
|
|
815
|
187 AVFrame *pic= av_mallocz(sizeof(AVFrame));
|
|
|
188
|
|
808
|
189 return pic;
|
|
|
190 }
|
|
|
191
|
|
|
192 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
|
|
|
193 {
|
|
815
|
194 int ret;
|
|
808
|
195
|
|
|
196 if(avctx->codec)
|
|
815
|
197 return -1;
|
|
808
|
198
|
|
|
199 avctx->codec = codec;
|
|
|
200 avctx->codec_id = codec->id;
|
|
|
201 avctx->frame_number = 0;
|
|
815
|
202 if (codec->priv_data_size > 0) {
|
|
|
203 avctx->priv_data = av_mallocz(codec->priv_data_size);
|
|
|
204 if (!avctx->priv_data)
|
|
|
205 return -ENOMEM;
|
|
|
206 } else {
|
|
|
207 avctx->priv_data = NULL;
|
|
|
208 }
|
|
808
|
209 ret = avctx->codec->init(avctx);
|
|
|
210 if (ret < 0) {
|
|
|
211 av_freep(&avctx->priv_data);
|
|
815
|
212 return ret;
|
|
808
|
213 }
|
|
815
|
214 return 0;
|
|
808
|
215 }
|
|
|
216
|
|
815
|
217 int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
|
|
|
218 const short *samples)
|
|
808
|
219 {
|
|
|
220 int ret;
|
|
|
221
|
|
815
|
222 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
|
|
|
223 avctx->frame_number++;
|
|
808
|
224 return ret;
|
|
|
225 }
|
|
|
226
|
|
|
227 /* decode an audio frame. return -1 if error, otherwise return the
|
|
|
228 *number of bytes used. If no frame could be decompressed,
|
|
|
229 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
|
|
|
230 *size in BYTES. */
|
|
815
|
231 int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
|
|
808
|
232 int *frame_size_ptr,
|
|
|
233 uint8_t *buf, int buf_size)
|
|
|
234 {
|
|
|
235 int ret;
|
|
815
|
236 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
|
|
|
237 buf, buf_size);
|
|
|
238 avctx->frame_number++;
|
|
808
|
239 return ret;
|
|
|
240 }
|
|
|
241
|
|
|
242 int avcodec_close(AVCodecContext *avctx)
|
|
|
243 {
|
|
|
244 if (avctx->codec->close)
|
|
|
245 avctx->codec->close(avctx);
|
|
|
246 av_freep(&avctx->priv_data);
|
|
|
247 avctx->codec = NULL;
|
|
|
248 return 0;
|
|
|
249 }
|
|
|
250
|
|
|
251 AVCodec *avcodec_find_encoder(enum CodecID id)
|
|
|
252 {
|
|
|
253 AVCodec *p;
|
|
|
254 p = first_avcodec;
|
|
|
255 while (p) {
|
|
815
|
256 if (p->encode != NULL && (enum CodecID)p->id == id)
|
|
808
|
257 return p;
|
|
|
258 p = p->next;
|
|
|
259 }
|
|
|
260 return NULL;
|
|
|
261 }
|
|
|
262
|
|
|
263 AVCodec *avcodec_find_encoder_by_name(const char *name)
|
|
|
264 {
|
|
|
265 AVCodec *p;
|
|
|
266 p = first_avcodec;
|
|
|
267 while (p) {
|
|
|
268 if (p->encode != NULL && strcmp(name,p->name) == 0)
|
|
|
269 return p;
|
|
|
270 p = p->next;
|
|
|
271 }
|
|
|
272 return NULL;
|
|
|
273 }
|
|
|
274
|
|
|
275 AVCodec *avcodec_find_decoder(enum CodecID id)
|
|
|
276 {
|
|
|
277 AVCodec *p;
|
|
|
278 p = first_avcodec;
|
|
|
279 while (p) {
|
|
815
|
280 if (p->decode != NULL && (enum CodecID)p->id == id)
|
|
808
|
281 return p;
|
|
|
282 p = p->next;
|
|
|
283 }
|
|
|
284 return NULL;
|
|
|
285 }
|
|
|
286
|
|
|
287 AVCodec *avcodec_find_decoder_by_name(const char *name)
|
|
|
288 {
|
|
|
289 AVCodec *p;
|
|
|
290 p = first_avcodec;
|
|
|
291 while (p) {
|
|
|
292 if (p->decode != NULL && strcmp(name,p->name) == 0)
|
|
|
293 return p;
|
|
|
294 p = p->next;
|
|
|
295 }
|
|
|
296 return NULL;
|
|
|
297 }
|
|
|
298
|
|
815
|
299 AVCodec *avcodec_find(enum CodecID id)
|
|
|
300 {
|
|
|
301 AVCodec *p;
|
|
|
302 p = first_avcodec;
|
|
|
303 while (p) {
|
|
|
304 if ((enum CodecID)p->id == id)
|
|
|
305 return p;
|
|
|
306 p = p->next;
|
|
|
307 }
|
|
|
308 return NULL;
|
|
|
309 }
|
|
|
310
|
|
808
|
311 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
|
|
|
312 {
|
|
|
313 const char *codec_name;
|
|
|
314 AVCodec *p;
|
|
|
315 char buf1[32];
|
|
|
316 char channels_str[100];
|
|
|
317 int bitrate;
|
|
|
318
|
|
|
319 if (encode)
|
|
|
320 p = avcodec_find_encoder(enc->codec_id);
|
|
|
321 else
|
|
|
322 p = avcodec_find_decoder(enc->codec_id);
|
|
|
323
|
|
|
324 if (p) {
|
|
|
325 codec_name = p->name;
|
|
|
326 } else if (enc->codec_name[0] != '\0') {
|
|
|
327 codec_name = enc->codec_name;
|
|
|
328 } else {
|
|
|
329 /* output avi tags */
|
|
815
|
330 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
|
|
808
|
331 codec_name = buf1;
|
|
|
332 }
|
|
|
333
|
|
|
334 switch(enc->codec_type) {
|
|
|
335 case CODEC_TYPE_AUDIO:
|
|
|
336 snprintf(buf, buf_size,
|
|
|
337 "Audio: %s",
|
|
|
338 codec_name);
|
|
|
339 switch (enc->channels) {
|
|
|
340 case 1:
|
|
|
341 strcpy(channels_str, "mono");
|
|
|
342 break;
|
|
|
343 case 2:
|
|
|
344 strcpy(channels_str, "stereo");
|
|
|
345 break;
|
|
|
346 case 6:
|
|
|
347 strcpy(channels_str, "5:1");
|
|
|
348 break;
|
|
|
349 default:
|
|
818
|
350 snprintf(channels_str, 100, "%d channels", enc->channels);
|
|
808
|
351 break;
|
|
|
352 }
|
|
|
353 if (enc->sample_rate) {
|
|
|
354 snprintf(buf + strlen(buf), buf_size - strlen(buf),
|
|
|
355 ", %d Hz, %s",
|
|
|
356 enc->sample_rate,
|
|
|
357 channels_str);
|
|
|
358 }
|
|
815
|
359
|
|
808
|
360 /* for PCM codecs, compute bitrate directly */
|
|
|
361 switch(enc->codec_id) {
|
|
|
362 case CODEC_ID_PCM_S16LE:
|
|
|
363 case CODEC_ID_PCM_S16BE:
|
|
|
364 case CODEC_ID_PCM_U16LE:
|
|
|
365 case CODEC_ID_PCM_U16BE:
|
|
|
366 bitrate = enc->sample_rate * enc->channels * 16;
|
|
|
367 break;
|
|
|
368 case CODEC_ID_PCM_S8:
|
|
|
369 case CODEC_ID_PCM_U8:
|
|
|
370 case CODEC_ID_PCM_ALAW:
|
|
|
371 case CODEC_ID_PCM_MULAW:
|
|
|
372 bitrate = enc->sample_rate * enc->channels * 8;
|
|
|
373 break;
|
|
|
374 default:
|
|
|
375 bitrate = enc->bit_rate;
|
|
|
376 break;
|
|
|
377 }
|
|
|
378 break;
|
|
|
379 case CODEC_TYPE_DATA:
|
|
|
380 snprintf(buf, buf_size, "Data: %s", codec_name);
|
|
|
381 bitrate = enc->bit_rate;
|
|
|
382 break;
|
|
|
383 default:
|
|
815
|
384 av_abort();
|
|
808
|
385 }
|
|
|
386 if (encode) {
|
|
|
387 if (enc->flags & CODEC_FLAG_PASS1)
|
|
|
388 snprintf(buf + strlen(buf), buf_size - strlen(buf),
|
|
|
389 ", pass 1");
|
|
|
390 if (enc->flags & CODEC_FLAG_PASS2)
|
|
|
391 snprintf(buf + strlen(buf), buf_size - strlen(buf),
|
|
|
392 ", pass 2");
|
|
|
393 }
|
|
|
394 if (bitrate != 0) {
|
|
815
|
395 snprintf(buf + strlen(buf), buf_size - strlen(buf),
|
|
808
|
396 ", %d kb/s", bitrate / 1000);
|
|
|
397 }
|
|
|
398 }
|
|
|
399
|
|
|
400 unsigned avcodec_version( void )
|
|
|
401 {
|
|
|
402 return LIBAVCODEC_VERSION_INT;
|
|
|
403 }
|
|
|
404
|
|
|
405 unsigned avcodec_build( void )
|
|
|
406 {
|
|
|
407 return LIBAVCODEC_BUILD;
|
|
|
408 }
|
|
|
409
|
|
|
410 /* must be called before any other functions */
|
|
|
411 void avcodec_init(void)
|
|
|
412 {
|
|
|
413 static int inited = 0;
|
|
|
414
|
|
|
415 if (inited != 0)
|
|
815
|
416 return;
|
|
808
|
417 inited = 1;
|
|
|
418
|
|
|
419 dsputil_static_init();
|
|
|
420 }
|
|
|
421
|
|
|
422 /**
|
|
|
423 * Flush buffers, should be called when seeking or when swicthing to a different stream.
|
|
|
424 */
|
|
|
425 void avcodec_flush_buffers(AVCodecContext *avctx)
|
|
|
426 {
|
|
|
427 if(avctx->codec->flush)
|
|
|
428 avctx->codec->flush(avctx);
|
|
|
429 }
|
|
|
430
|
|
|
431 void avcodec_default_free_buffers(AVCodecContext *s){
|
|
|
432 int i, j;
|
|
|
433
|
|
|
434 if(s->internal_buffer==NULL) return;
|
|
815
|
435
|
|
808
|
436 for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
|
|
|
437 InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
|
|
|
438 for(j=0; j<4; j++){
|
|
|
439 av_freep(&buf->base[j]);
|
|
|
440 buf->data[j]= NULL;
|
|
|
441 }
|
|
|
442 }
|
|
|
443 av_freep(&s->internal_buffer);
|
|
815
|
444
|
|
808
|
445 s->internal_buffer_count=0;
|
|
|
446 }
|
|
815
|
447 #if 0
|
|
808
|
448 char av_get_pict_type_char(int pict_type){
|
|
|
449 switch(pict_type){
|
|
815
|
450 case I_TYPE: return 'I';
|
|
|
451 case P_TYPE: return 'P';
|
|
|
452 case B_TYPE: return 'B';
|
|
|
453 case S_TYPE: return 'S';
|
|
|
454 case SI_TYPE:return 'i';
|
|
|
455 case SP_TYPE:return 'p';
|
|
808
|
456 default: return '?';
|
|
|
457 }
|
|
|
458 }
|
|
|
459
|
|
815
|
460 int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
|
|
|
461 int exact=1, sign=0;
|
|
|
462 int64_t gcd;
|
|
|
463
|
|
|
464 assert(den != 0);
|
|
|
465
|
|
|
466 if(den < 0){
|
|
|
467 den= -den;
|
|
|
468 nom= -nom;
|
|
|
469 }
|
|
|
470
|
|
|
471 if(nom < 0){
|
|
|
472 nom= -nom;
|
|
|
473 sign= 1;
|
|
808
|
474 }
|
|
815
|
475
|
|
|
476 gcd = ff_gcd(nom, den);
|
|
|
477 nom /= gcd;
|
|
|
478 den /= gcd;
|
|
|
479
|
|
|
480 if(nom > max || den > max){
|
|
|
481 AVRational a0={0,1}, a1={1,0};
|
|
|
482 exact=0;
|
|
|
483
|
|
|
484 for(;;){
|
|
|
485 int64_t x= nom / den;
|
|
|
486 int64_t a2n= x*a1.num + a0.num;
|
|
|
487 int64_t a2d= x*a1.den + a0.den;
|
|
|
488
|
|
|
489 if(a2n > max || a2d > max) break;
|
|
|
490
|
|
|
491 nom %= den;
|
|
|
492
|
|
|
493 a0= a1;
|
|
|
494 a1= (AVRational){a2n, a2d};
|
|
|
495 if(nom==0) break;
|
|
|
496 x= nom; nom=den; den=x;
|
|
|
497 }
|
|
|
498 nom= a1.num;
|
|
|
499 den= a1.den;
|
|
|
500 }
|
|
|
501
|
|
|
502 assert(ff_gcd(nom, den) == 1);
|
|
|
503
|
|
|
504 if(sign) nom= -nom;
|
|
|
505
|
|
|
506 *dst_nom = nom;
|
|
|
507 *dst_den = den;
|
|
|
508
|
|
|
509 return exact;
|
|
|
510 }
|
|
|
511 #endif
|