You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

701 lines
18KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file utils.c
  21. * utils.
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. void *av_mallocz(unsigned int size)
  27. {
  28. void *ptr;
  29. ptr = av_malloc(size);
  30. if (!ptr)
  31. return NULL;
  32. memset(ptr, 0, size);
  33. return ptr;
  34. }
  35. char *av_strdup(const char *s)
  36. {
  37. char *ptr;
  38. int len;
  39. len = strlen(s) + 1;
  40. ptr = av_malloc(len);
  41. if (!ptr)
  42. return NULL;
  43. memcpy(ptr, s, len);
  44. return ptr;
  45. }
  46. /**
  47. * realloc which does nothing if the block is large enough
  48. */
  49. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  50. {
  51. if(min_size < *size)
  52. return ptr;
  53. *size= min_size + 10*1024;
  54. return av_realloc(ptr, *size);
  55. }
  56. /* allocation of static arrays - do not use for normal allocation */
  57. static unsigned int last_static = 0;
  58. static char*** array_static = NULL;
  59. static const unsigned int grow_static = 64; // ^2
  60. void *__av_mallocz_static(void** location, unsigned int size)
  61. {
  62. unsigned int l = (last_static + grow_static) & ~(grow_static - 1);
  63. void *ptr = av_mallocz(size);
  64. if (!ptr)
  65. return NULL;
  66. if (location)
  67. {
  68. if (l > last_static)
  69. array_static = av_realloc(array_static, l);
  70. array_static[last_static++] = (char**) location;
  71. *location = ptr;
  72. }
  73. return ptr;
  74. }
  75. /* free all static arrays and reset pointers to 0 */
  76. void av_free_static()
  77. {
  78. if (array_static)
  79. {
  80. unsigned i;
  81. for (i = 0; i < last_static; i++)
  82. {
  83. av_free(*array_static[i]);
  84. *array_static[i] = NULL;
  85. }
  86. av_free(array_static);
  87. array_static = 0;
  88. }
  89. last_static = 0;
  90. }
  91. /* cannot call it directly because of 'void **' casting is not automatic */
  92. void __av_freep(void **ptr)
  93. {
  94. av_free(*ptr);
  95. *ptr = NULL;
  96. }
  97. /* encoder management */
  98. AVCodec *first_avcodec;
  99. void register_avcodec(AVCodec *format)
  100. {
  101. AVCodec **p;
  102. p = &first_avcodec;
  103. while (*p != NULL) p = &(*p)->next;
  104. *p = format;
  105. format->next = NULL;
  106. }
  107. typedef struct InternalBuffer{
  108. int last_pic_num;
  109. uint8_t *base[4];
  110. uint8_t *data[4];
  111. }InternalBuffer;
  112. #define INTERNAL_BUFFER_SIZE 32
  113. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  114. int i;
  115. const int width = s->width;
  116. const int height= s->height;
  117. InternalBuffer *buf;
  118. assert(pic->data[0]==NULL);
  119. assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
  120. if(s->internal_buffer==NULL){
  121. s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
  122. }
  123. #if 0
  124. s->internal_buffer= av_fast_realloc(
  125. s->internal_buffer,
  126. &s->internal_buffer_size,
  127. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  128. );
  129. #endif
  130. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  131. if(buf->base[0]){
  132. pic->age= pic->coded_picture_number - buf->last_pic_num;
  133. buf->last_pic_num= pic->coded_picture_number;
  134. }else{
  135. int align, h_chroma_shift, v_chroma_shift;
  136. int w, h, pixel_size;
  137. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  138. switch(s->pix_fmt){
  139. case PIX_FMT_YUV422:
  140. pixel_size=2;
  141. break;
  142. case PIX_FMT_RGB24:
  143. case PIX_FMT_BGR24:
  144. pixel_size=3;
  145. break;
  146. case PIX_FMT_RGBA32:
  147. pixel_size=4;
  148. break;
  149. default:
  150. pixel_size=1;
  151. }
  152. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  153. else align=15;
  154. w= (width +align)&~align;
  155. h= (height+align)&~align;
  156. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  157. w+= EDGE_WIDTH*2;
  158. h+= EDGE_WIDTH*2;
  159. }
  160. buf->last_pic_num= -256*256*256*64;
  161. for(i=0; i<3; i++){
  162. const int h_shift= i==0 ? 0 : h_chroma_shift;
  163. const int v_shift= i==0 ? 0 : v_chroma_shift;
  164. pic->linesize[i]= pixel_size*w>>h_shift;
  165. buf->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  166. if(buf->base[i]==NULL) return -1;
  167. memset(buf->base[i], 128, pic->linesize[i]*h>>v_shift);
  168. if(s->flags&CODEC_FLAG_EMU_EDGE)
  169. buf->data[i] = buf->base[i];
  170. else
  171. buf->data[i] = buf->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift);
  172. }
  173. pic->age= 256*256*256*64;
  174. pic->type= FF_BUFFER_TYPE_INTERNAL;
  175. }
  176. for(i=0; i<4; i++){
  177. pic->base[i]= buf->base[i];
  178. pic->data[i]= buf->data[i];
  179. }
  180. s->internal_buffer_count++;
  181. return 0;
  182. }
  183. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  184. int i;
  185. InternalBuffer *buf, *last, temp;
  186. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  187. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  188. buf= &((InternalBuffer*)s->internal_buffer)[i];
  189. if(buf->data[0] == pic->data[0])
  190. break;
  191. }
  192. assert(i < s->internal_buffer_count);
  193. s->internal_buffer_count--;
  194. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  195. temp= *buf;
  196. *buf= *last;
  197. *last= temp;
  198. for(i=0; i<3; i++){
  199. pic->data[i]=NULL;
  200. // pic->base[i]=NULL;
  201. }
  202. //printf("R%X\n", pic->opaque);
  203. }
  204. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
  205. return fmt[0];
  206. }
  207. void avcodec_get_context_defaults(AVCodecContext *s){
  208. s->bit_rate= 800*1000;
  209. s->bit_rate_tolerance= s->bit_rate*10;
  210. s->qmin= 2;
  211. s->qmax= 31;
  212. s->mb_qmin= 2;
  213. s->mb_qmax= 31;
  214. s->rc_eq= "tex^qComp";
  215. s->qcompress= 0.5;
  216. s->max_qdiff= 3;
  217. s->b_quant_factor=1.25;
  218. s->b_quant_offset=1.25;
  219. s->i_quant_factor=-0.8;
  220. s->i_quant_offset=0.0;
  221. s->error_concealment= 3;
  222. s->error_resilience= 1;
  223. s->workaround_bugs= FF_BUG_AUTODETECT;
  224. s->frame_rate_base= 1;
  225. s->frame_rate = 25;
  226. s->gop_size= 50;
  227. s->me_method= ME_EPZS;
  228. s->get_buffer= avcodec_default_get_buffer;
  229. s->release_buffer= avcodec_default_release_buffer;
  230. s->get_format= avcodec_default_get_format;
  231. s->me_subpel_quality=8;
  232. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  233. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  234. }
  235. /**
  236. * allocates a AVCodecContext and set it to defaults.
  237. * this can be deallocated by simply calling free()
  238. */
  239. AVCodecContext *avcodec_alloc_context(void){
  240. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  241. if(avctx==NULL) return NULL;
  242. avcodec_get_context_defaults(avctx);
  243. return avctx;
  244. }
  245. /**
  246. * allocates a AVPFrame and set it to defaults.
  247. * this can be deallocated by simply calling free()
  248. */
  249. AVFrame *avcodec_alloc_frame(void){
  250. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  251. return pic;
  252. }
  253. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  254. {
  255. int ret;
  256. avctx->codec = codec;
  257. avctx->codec_id = codec->id;
  258. avctx->frame_number = 0;
  259. if (codec->priv_data_size > 0) {
  260. avctx->priv_data = av_mallocz(codec->priv_data_size);
  261. if (!avctx->priv_data)
  262. return -ENOMEM;
  263. } else {
  264. avctx->priv_data = NULL;
  265. }
  266. ret = avctx->codec->init(avctx);
  267. if (ret < 0) {
  268. av_freep(&avctx->priv_data);
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  274. const short *samples)
  275. {
  276. int ret;
  277. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  278. avctx->frame_number++;
  279. return ret;
  280. }
  281. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  282. const AVFrame *pict)
  283. {
  284. int ret;
  285. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  286. emms_c(); //needed to avoid a emms_c() call before every return;
  287. avctx->frame_number++;
  288. return ret;
  289. }
  290. /**
  291. * decode a frame.
  292. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  293. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  294. * @param buf_size the size of the buffer in bytes
  295. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  296. * @return -1 if error, otherwise return the number of
  297. * bytes used.
  298. */
  299. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  300. int *got_picture_ptr,
  301. uint8_t *buf, int buf_size)
  302. {
  303. int ret;
  304. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  305. buf, buf_size);
  306. emms_c(); //needed to avoid a emms_c() call before every return;
  307. if (*got_picture_ptr)
  308. avctx->frame_number++;
  309. return ret;
  310. }
  311. /* decode an audio frame. return -1 if error, otherwise return the
  312. *number of bytes used. If no frame could be decompressed,
  313. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  314. *size in BYTES. */
  315. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  316. int *frame_size_ptr,
  317. uint8_t *buf, int buf_size)
  318. {
  319. int ret;
  320. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  321. buf, buf_size);
  322. avctx->frame_number++;
  323. return ret;
  324. }
  325. int avcodec_close(AVCodecContext *avctx)
  326. {
  327. if (avctx->codec->close)
  328. avctx->codec->close(avctx);
  329. av_freep(&avctx->priv_data);
  330. avctx->codec = NULL;
  331. return 0;
  332. }
  333. AVCodec *avcodec_find_encoder(enum CodecID id)
  334. {
  335. AVCodec *p;
  336. p = first_avcodec;
  337. while (p) {
  338. if (p->encode != NULL && p->id == id)
  339. return p;
  340. p = p->next;
  341. }
  342. return NULL;
  343. }
  344. AVCodec *avcodec_find_encoder_by_name(const char *name)
  345. {
  346. AVCodec *p;
  347. p = first_avcodec;
  348. while (p) {
  349. if (p->encode != NULL && strcmp(name,p->name) == 0)
  350. return p;
  351. p = p->next;
  352. }
  353. return NULL;
  354. }
  355. AVCodec *avcodec_find_decoder(enum CodecID id)
  356. {
  357. AVCodec *p;
  358. p = first_avcodec;
  359. while (p) {
  360. if (p->decode != NULL && p->id == id)
  361. return p;
  362. p = p->next;
  363. }
  364. return NULL;
  365. }
  366. AVCodec *avcodec_find_decoder_by_name(const char *name)
  367. {
  368. AVCodec *p;
  369. p = first_avcodec;
  370. while (p) {
  371. if (p->decode != NULL && strcmp(name,p->name) == 0)
  372. return p;
  373. p = p->next;
  374. }
  375. return NULL;
  376. }
  377. AVCodec *avcodec_find(enum CodecID id)
  378. {
  379. AVCodec *p;
  380. p = first_avcodec;
  381. while (p) {
  382. if (p->id == id)
  383. return p;
  384. p = p->next;
  385. }
  386. return NULL;
  387. }
  388. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  389. {
  390. const char *codec_name;
  391. AVCodec *p;
  392. char buf1[32];
  393. char channels_str[100];
  394. int bitrate;
  395. if (encode)
  396. p = avcodec_find_encoder(enc->codec_id);
  397. else
  398. p = avcodec_find_decoder(enc->codec_id);
  399. if (p) {
  400. codec_name = p->name;
  401. } else if (enc->codec_name[0] != '\0') {
  402. codec_name = enc->codec_name;
  403. } else {
  404. /* output avi tags */
  405. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  406. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  407. enc->codec_tag & 0xff,
  408. (enc->codec_tag >> 8) & 0xff,
  409. (enc->codec_tag >> 16) & 0xff,
  410. (enc->codec_tag >> 24) & 0xff);
  411. } else {
  412. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  413. }
  414. codec_name = buf1;
  415. }
  416. switch(enc->codec_type) {
  417. case CODEC_TYPE_VIDEO:
  418. snprintf(buf, buf_size,
  419. "Video: %s%s",
  420. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  421. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  422. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  423. ", %s",
  424. avcodec_get_pix_fmt_name(enc->pix_fmt));
  425. }
  426. if (enc->width) {
  427. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  428. ", %dx%d, %0.2f fps",
  429. enc->width, enc->height,
  430. (float)enc->frame_rate / enc->frame_rate_base);
  431. }
  432. if (encode) {
  433. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  434. ", q=%d-%d", enc->qmin, enc->qmax);
  435. }
  436. bitrate = enc->bit_rate;
  437. break;
  438. case CODEC_TYPE_AUDIO:
  439. snprintf(buf, buf_size,
  440. "Audio: %s",
  441. codec_name);
  442. switch (enc->channels) {
  443. case 1:
  444. strcpy(channels_str, "mono");
  445. break;
  446. case 2:
  447. strcpy(channels_str, "stereo");
  448. break;
  449. case 6:
  450. strcpy(channels_str, "5:1");
  451. break;
  452. default:
  453. sprintf(channels_str, "%d channels", enc->channels);
  454. break;
  455. }
  456. if (enc->sample_rate) {
  457. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  458. ", %d Hz, %s",
  459. enc->sample_rate,
  460. channels_str);
  461. }
  462. /* for PCM codecs, compute bitrate directly */
  463. switch(enc->codec_id) {
  464. case CODEC_ID_PCM_S16LE:
  465. case CODEC_ID_PCM_S16BE:
  466. case CODEC_ID_PCM_U16LE:
  467. case CODEC_ID_PCM_U16BE:
  468. bitrate = enc->sample_rate * enc->channels * 16;
  469. break;
  470. case CODEC_ID_PCM_S8:
  471. case CODEC_ID_PCM_U8:
  472. case CODEC_ID_PCM_ALAW:
  473. case CODEC_ID_PCM_MULAW:
  474. bitrate = enc->sample_rate * enc->channels * 8;
  475. break;
  476. default:
  477. bitrate = enc->bit_rate;
  478. break;
  479. }
  480. break;
  481. default:
  482. av_abort();
  483. }
  484. if (encode) {
  485. if (enc->flags & CODEC_FLAG_PASS1)
  486. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  487. ", pass 1");
  488. if (enc->flags & CODEC_FLAG_PASS2)
  489. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  490. ", pass 2");
  491. }
  492. if (bitrate != 0) {
  493. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  494. ", %d kb/s", bitrate / 1000);
  495. }
  496. }
  497. unsigned avcodec_version( void )
  498. {
  499. return LIBAVCODEC_VERSION_INT;
  500. }
  501. unsigned avcodec_build( void )
  502. {
  503. return LIBAVCODEC_BUILD;
  504. }
  505. /* must be called before any other functions */
  506. void avcodec_init(void)
  507. {
  508. static int inited = 0;
  509. if (inited != 0)
  510. return;
  511. inited = 1;
  512. dsputil_static_init();
  513. }
  514. /* this can be called after seeking and before trying to decode the next keyframe */
  515. void avcodec_flush_buffers(AVCodecContext *avctx)
  516. {
  517. int i;
  518. MpegEncContext *s = avctx->priv_data;
  519. switch(avctx->codec_id){
  520. case CODEC_ID_MPEG1VIDEO:
  521. case CODEC_ID_H263:
  522. case CODEC_ID_RV10:
  523. // case CODEC_ID_MJPEG:
  524. // case CODEC_ID_MJPEGB:
  525. case CODEC_ID_MPEG4:
  526. case CODEC_ID_MSMPEG4V1:
  527. case CODEC_ID_MSMPEG4V2:
  528. case CODEC_ID_MSMPEG4V3:
  529. case CODEC_ID_WMV1:
  530. case CODEC_ID_WMV2:
  531. case CODEC_ID_H263P:
  532. case CODEC_ID_H263I:
  533. case CODEC_ID_SVQ1:
  534. for(i=0; i<MAX_PICTURE_COUNT; i++){
  535. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  536. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  537. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  538. }
  539. s->last_picture_ptr = s->next_picture_ptr = NULL;
  540. break;
  541. default:
  542. //FIXME
  543. break;
  544. }
  545. }
  546. void avcodec_default_free_buffers(AVCodecContext *s){
  547. int i, j;
  548. if(s->internal_buffer==NULL) return;
  549. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  550. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  551. for(j=0; j<4; j++){
  552. av_freep(&buf->base[j]);
  553. buf->data[j]= NULL;
  554. }
  555. }
  556. av_freep(&s->internal_buffer);
  557. s->internal_buffer_count=0;
  558. }
  559. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  560. int exact=1, sign=0;
  561. int64_t gcd, larger;
  562. assert(den != 0);
  563. if(den < 0){
  564. den= -den;
  565. nom= -nom;
  566. }
  567. if(nom < 0){
  568. nom= -nom;
  569. sign= 1;
  570. }
  571. for(;;){ //note is executed 1 or 2 times
  572. gcd = ff_gcd(nom, den);
  573. nom /= gcd;
  574. den /= gcd;
  575. larger= FFMAX(nom, den);
  576. if(larger > max){
  577. int64_t div= (larger + max - 1) / max;
  578. nom = (nom + div/2)/div;
  579. den = (den + div/2)/div;
  580. exact=0;
  581. }else
  582. break;
  583. }
  584. if(sign) nom= -nom;
  585. *dst_nom = nom;
  586. *dst_den = den;
  587. return exact;
  588. }
  589. int64_t av_rescale(int64_t a, int b, int c){
  590. uint64_t h, l;
  591. assert(c > 0);
  592. assert(b >=0);
  593. if(a<0) return -av_rescale(-a, b, c);
  594. h= a>>32;
  595. if(h==0) return a*b/c;
  596. l= a&0xFFFFFFFF;
  597. l *= b;
  598. h *= b;
  599. l += (h%c)<<32;
  600. return ((h/c)<<32) + l/c;
  601. }