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.

695 lines
17KB

  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. /* decode a frame. return -1 if error, otherwise return the number of
  291. bytes used. If no frame could be decompressed, *got_picture_ptr is
  292. zero. Otherwise, it is non zero */
  293. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  294. int *got_picture_ptr,
  295. uint8_t *buf, int buf_size)
  296. {
  297. int ret;
  298. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  299. buf, buf_size);
  300. emms_c(); //needed to avoid a emms_c() call before every return;
  301. if (*got_picture_ptr)
  302. avctx->frame_number++;
  303. return ret;
  304. }
  305. /* decode an audio frame. return -1 if error, otherwise return the
  306. *number of bytes used. If no frame could be decompressed,
  307. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  308. *size in BYTES. */
  309. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  310. int *frame_size_ptr,
  311. uint8_t *buf, int buf_size)
  312. {
  313. int ret;
  314. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  315. buf, buf_size);
  316. avctx->frame_number++;
  317. return ret;
  318. }
  319. int avcodec_close(AVCodecContext *avctx)
  320. {
  321. if (avctx->codec->close)
  322. avctx->codec->close(avctx);
  323. av_freep(&avctx->priv_data);
  324. avctx->codec = NULL;
  325. return 0;
  326. }
  327. AVCodec *avcodec_find_encoder(enum CodecID id)
  328. {
  329. AVCodec *p;
  330. p = first_avcodec;
  331. while (p) {
  332. if (p->encode != NULL && p->id == id)
  333. return p;
  334. p = p->next;
  335. }
  336. return NULL;
  337. }
  338. AVCodec *avcodec_find_encoder_by_name(const char *name)
  339. {
  340. AVCodec *p;
  341. p = first_avcodec;
  342. while (p) {
  343. if (p->encode != NULL && strcmp(name,p->name) == 0)
  344. return p;
  345. p = p->next;
  346. }
  347. return NULL;
  348. }
  349. AVCodec *avcodec_find_decoder(enum CodecID id)
  350. {
  351. AVCodec *p;
  352. p = first_avcodec;
  353. while (p) {
  354. if (p->decode != NULL && p->id == id)
  355. return p;
  356. p = p->next;
  357. }
  358. return NULL;
  359. }
  360. AVCodec *avcodec_find_decoder_by_name(const char *name)
  361. {
  362. AVCodec *p;
  363. p = first_avcodec;
  364. while (p) {
  365. if (p->decode != NULL && strcmp(name,p->name) == 0)
  366. return p;
  367. p = p->next;
  368. }
  369. return NULL;
  370. }
  371. AVCodec *avcodec_find(enum CodecID id)
  372. {
  373. AVCodec *p;
  374. p = first_avcodec;
  375. while (p) {
  376. if (p->id == id)
  377. return p;
  378. p = p->next;
  379. }
  380. return NULL;
  381. }
  382. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  383. {
  384. const char *codec_name;
  385. AVCodec *p;
  386. char buf1[32];
  387. char channels_str[100];
  388. int bitrate;
  389. if (encode)
  390. p = avcodec_find_encoder(enc->codec_id);
  391. else
  392. p = avcodec_find_decoder(enc->codec_id);
  393. if (p) {
  394. codec_name = p->name;
  395. } else if (enc->codec_name[0] != '\0') {
  396. codec_name = enc->codec_name;
  397. } else {
  398. /* output avi tags */
  399. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  400. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  401. enc->codec_tag & 0xff,
  402. (enc->codec_tag >> 8) & 0xff,
  403. (enc->codec_tag >> 16) & 0xff,
  404. (enc->codec_tag >> 24) & 0xff);
  405. } else {
  406. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  407. }
  408. codec_name = buf1;
  409. }
  410. switch(enc->codec_type) {
  411. case CODEC_TYPE_VIDEO:
  412. snprintf(buf, buf_size,
  413. "Video: %s%s",
  414. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  415. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  416. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  417. ", %s",
  418. avcodec_get_pix_fmt_name(enc->pix_fmt));
  419. }
  420. if (enc->width) {
  421. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  422. ", %dx%d, %0.2f fps",
  423. enc->width, enc->height,
  424. (float)enc->frame_rate / enc->frame_rate_base);
  425. }
  426. if (encode) {
  427. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  428. ", q=%d-%d", enc->qmin, enc->qmax);
  429. }
  430. bitrate = enc->bit_rate;
  431. break;
  432. case CODEC_TYPE_AUDIO:
  433. snprintf(buf, buf_size,
  434. "Audio: %s",
  435. codec_name);
  436. switch (enc->channels) {
  437. case 1:
  438. strcpy(channels_str, "mono");
  439. break;
  440. case 2:
  441. strcpy(channels_str, "stereo");
  442. break;
  443. case 6:
  444. strcpy(channels_str, "5:1");
  445. break;
  446. default:
  447. sprintf(channels_str, "%d channels", enc->channels);
  448. break;
  449. }
  450. if (enc->sample_rate) {
  451. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  452. ", %d Hz, %s",
  453. enc->sample_rate,
  454. channels_str);
  455. }
  456. /* for PCM codecs, compute bitrate directly */
  457. switch(enc->codec_id) {
  458. case CODEC_ID_PCM_S16LE:
  459. case CODEC_ID_PCM_S16BE:
  460. case CODEC_ID_PCM_U16LE:
  461. case CODEC_ID_PCM_U16BE:
  462. bitrate = enc->sample_rate * enc->channels * 16;
  463. break;
  464. case CODEC_ID_PCM_S8:
  465. case CODEC_ID_PCM_U8:
  466. case CODEC_ID_PCM_ALAW:
  467. case CODEC_ID_PCM_MULAW:
  468. bitrate = enc->sample_rate * enc->channels * 8;
  469. break;
  470. default:
  471. bitrate = enc->bit_rate;
  472. break;
  473. }
  474. break;
  475. default:
  476. av_abort();
  477. }
  478. if (encode) {
  479. if (enc->flags & CODEC_FLAG_PASS1)
  480. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  481. ", pass 1");
  482. if (enc->flags & CODEC_FLAG_PASS2)
  483. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  484. ", pass 2");
  485. }
  486. if (bitrate != 0) {
  487. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  488. ", %d kb/s", bitrate / 1000);
  489. }
  490. }
  491. unsigned avcodec_version( void )
  492. {
  493. return LIBAVCODEC_VERSION_INT;
  494. }
  495. unsigned avcodec_build( void )
  496. {
  497. return LIBAVCODEC_BUILD;
  498. }
  499. /* must be called before any other functions */
  500. void avcodec_init(void)
  501. {
  502. static int inited = 0;
  503. if (inited != 0)
  504. return;
  505. inited = 1;
  506. dsputil_static_init();
  507. }
  508. /* this can be called after seeking and before trying to decode the next keyframe */
  509. void avcodec_flush_buffers(AVCodecContext *avctx)
  510. {
  511. int i;
  512. MpegEncContext *s = avctx->priv_data;
  513. switch(avctx->codec_id){
  514. case CODEC_ID_MPEG1VIDEO:
  515. case CODEC_ID_H263:
  516. case CODEC_ID_RV10:
  517. // case CODEC_ID_MJPEG:
  518. // case CODEC_ID_MJPEGB:
  519. case CODEC_ID_MPEG4:
  520. case CODEC_ID_MSMPEG4V1:
  521. case CODEC_ID_MSMPEG4V2:
  522. case CODEC_ID_MSMPEG4V3:
  523. case CODEC_ID_WMV1:
  524. case CODEC_ID_WMV2:
  525. case CODEC_ID_H263P:
  526. case CODEC_ID_H263I:
  527. case CODEC_ID_SVQ1:
  528. for(i=0; i<MAX_PICTURE_COUNT; i++){
  529. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  530. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  531. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  532. }
  533. s->last_picture_ptr = s->next_picture_ptr = NULL;
  534. break;
  535. default:
  536. //FIXME
  537. break;
  538. }
  539. }
  540. void avcodec_default_free_buffers(AVCodecContext *s){
  541. int i, j;
  542. if(s->internal_buffer==NULL) return;
  543. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  544. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  545. for(j=0; j<4; j++){
  546. av_freep(&buf->base[j]);
  547. buf->data[j]= NULL;
  548. }
  549. }
  550. av_freep(&s->internal_buffer);
  551. s->internal_buffer_count=0;
  552. }
  553. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  554. int exact=1, sign=0;
  555. int64_t gcd, larger;
  556. assert(den != 0);
  557. if(den < 0){
  558. den= -den;
  559. nom= -nom;
  560. }
  561. if(nom < 0){
  562. nom= -nom;
  563. sign= 1;
  564. }
  565. for(;;){ //note is executed 1 or 2 times
  566. gcd = ff_gcd(nom, den);
  567. nom /= gcd;
  568. den /= gcd;
  569. larger= FFMAX(nom, den);
  570. if(larger > max){
  571. int64_t div= (larger + max - 1) / max;
  572. nom = (nom + div/2)/div;
  573. den = (den + div/2)/div;
  574. exact=0;
  575. }else
  576. break;
  577. }
  578. if(sign) nom= -nom;
  579. *dst_nom = nom;
  580. *dst_den = den;
  581. return exact;
  582. }
  583. int64_t av_rescale(int64_t a, int b, int c){
  584. uint64_t h, l;
  585. assert(c > 0);
  586. assert(b >=0);
  587. if(a<0) return -av_rescale(-a, b, c);
  588. h= a>>32;
  589. if(h==0) return a*b/c;
  590. l= a&0xFFFFFFFF;
  591. l *= b;
  592. h *= b;
  593. l += (h%c)<<32;
  594. return ((h/c)<<32) + l/c;
  595. }