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.

742 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(void)
  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. #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  114. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  115. int w_align= 1;
  116. int h_align= 1;
  117. switch(s->pix_fmt){
  118. case PIX_FMT_YUV420P:
  119. case PIX_FMT_YUV422:
  120. case PIX_FMT_YUV422P:
  121. case PIX_FMT_YUV444P:
  122. case PIX_FMT_GRAY8:
  123. case PIX_FMT_YUVJ420P:
  124. case PIX_FMT_YUVJ422P:
  125. case PIX_FMT_YUVJ444P:
  126. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  127. h_align= 16;
  128. break;
  129. case PIX_FMT_YUV411P:
  130. w_align=32;
  131. h_align=8;
  132. break;
  133. case PIX_FMT_YUV410P:
  134. if(s->codec_id == CODEC_ID_SVQ1){
  135. w_align=64;
  136. h_align=64;
  137. }
  138. break;
  139. default:
  140. w_align= 1;
  141. h_align= 1;
  142. break;
  143. }
  144. *width = ALIGN(*width , w_align);
  145. *height= ALIGN(*height, h_align);
  146. }
  147. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  148. int i;
  149. int w= s->width;
  150. int h= s->height;
  151. InternalBuffer *buf;
  152. assert(pic->data[0]==NULL);
  153. assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
  154. if(s->internal_buffer==NULL){
  155. s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
  156. }
  157. #if 0
  158. s->internal_buffer= av_fast_realloc(
  159. s->internal_buffer,
  160. &s->internal_buffer_size,
  161. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  162. );
  163. #endif
  164. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  165. if(buf->base[0]){
  166. pic->age= pic->coded_picture_number - buf->last_pic_num;
  167. buf->last_pic_num= pic->coded_picture_number;
  168. }else{
  169. int h_chroma_shift, v_chroma_shift;
  170. int s_align, pixel_size;
  171. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  172. switch(s->pix_fmt){
  173. case PIX_FMT_RGB555:
  174. case PIX_FMT_RGB565:
  175. case PIX_FMT_YUV422:
  176. pixel_size=2;
  177. break;
  178. case PIX_FMT_RGB24:
  179. case PIX_FMT_BGR24:
  180. pixel_size=3;
  181. break;
  182. case PIX_FMT_RGBA32:
  183. pixel_size=4;
  184. break;
  185. default:
  186. pixel_size=1;
  187. }
  188. avcodec_align_dimensions(s, &w, &h);
  189. #if defined(ARCH_POWERPC) || defined(HAVE_MMI) //FIXME some cleaner check
  190. s_align= 16;
  191. #else
  192. s_align= 8;
  193. #endif
  194. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  195. w+= EDGE_WIDTH*2;
  196. h+= EDGE_WIDTH*2;
  197. }
  198. buf->last_pic_num= -256*256*256*64;
  199. for(i=0; i<3; i++){
  200. const int h_shift= i==0 ? 0 : h_chroma_shift;
  201. const int v_shift= i==0 ? 0 : v_chroma_shift;
  202. pic->linesize[i]= ALIGN(pixel_size*w>>h_shift, s_align);
  203. buf->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  204. if(buf->base[i]==NULL) return -1;
  205. memset(buf->base[i], 128, pic->linesize[i]*h>>v_shift);
  206. if(s->flags&CODEC_FLAG_EMU_EDGE)
  207. buf->data[i] = buf->base[i];
  208. else
  209. buf->data[i] = buf->base[i] + ALIGN((pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), s_align);
  210. }
  211. pic->age= 256*256*256*64;
  212. pic->type= FF_BUFFER_TYPE_INTERNAL;
  213. }
  214. for(i=0; i<4; i++){
  215. pic->base[i]= buf->base[i];
  216. pic->data[i]= buf->data[i];
  217. }
  218. s->internal_buffer_count++;
  219. return 0;
  220. }
  221. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  222. int i;
  223. InternalBuffer *buf, *last, temp;
  224. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  225. assert(s->internal_buffer_count);
  226. buf = NULL; /* avoids warning */
  227. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  228. buf= &((InternalBuffer*)s->internal_buffer)[i];
  229. if(buf->data[0] == pic->data[0])
  230. break;
  231. }
  232. assert(i < s->internal_buffer_count);
  233. s->internal_buffer_count--;
  234. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  235. temp= *buf;
  236. *buf= *last;
  237. *last= temp;
  238. for(i=0; i<3; i++){
  239. pic->data[i]=NULL;
  240. // pic->base[i]=NULL;
  241. }
  242. //printf("R%X\n", pic->opaque);
  243. }
  244. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
  245. return fmt[0];
  246. }
  247. void avcodec_get_context_defaults(AVCodecContext *s){
  248. s->bit_rate= 800*1000;
  249. s->bit_rate_tolerance= s->bit_rate*10;
  250. s->qmin= 2;
  251. s->qmax= 31;
  252. s->mb_qmin= 2;
  253. s->mb_qmax= 31;
  254. s->rc_eq= "tex^qComp";
  255. s->qcompress= 0.5;
  256. s->max_qdiff= 3;
  257. s->b_quant_factor=1.25;
  258. s->b_quant_offset=1.25;
  259. s->i_quant_factor=-0.8;
  260. s->i_quant_offset=0.0;
  261. s->error_concealment= 3;
  262. s->error_resilience= 1;
  263. s->workaround_bugs= FF_BUG_AUTODETECT;
  264. s->frame_rate_base= 1;
  265. s->frame_rate = 25;
  266. s->gop_size= 50;
  267. s->me_method= ME_EPZS;
  268. s->get_buffer= avcodec_default_get_buffer;
  269. s->release_buffer= avcodec_default_release_buffer;
  270. s->get_format= avcodec_default_get_format;
  271. s->me_subpel_quality=8;
  272. s->lmin= FF_QP2LAMBDA * s->qmin;
  273. s->lmax= FF_QP2LAMBDA * s->qmax;
  274. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  275. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  276. }
  277. /**
  278. * allocates a AVCodecContext and set it to defaults.
  279. * this can be deallocated by simply calling free()
  280. */
  281. AVCodecContext *avcodec_alloc_context(void){
  282. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  283. if(avctx==NULL) return NULL;
  284. avcodec_get_context_defaults(avctx);
  285. return avctx;
  286. }
  287. /**
  288. * allocates a AVPFrame and set it to defaults.
  289. * this can be deallocated by simply calling free()
  290. */
  291. AVFrame *avcodec_alloc_frame(void){
  292. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  293. return pic;
  294. }
  295. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  296. {
  297. int ret;
  298. if(avctx->codec)
  299. return -1;
  300. avctx->codec = codec;
  301. avctx->codec_id = codec->id;
  302. avctx->frame_number = 0;
  303. if (codec->priv_data_size > 0) {
  304. avctx->priv_data = av_mallocz(codec->priv_data_size);
  305. if (!avctx->priv_data)
  306. return -ENOMEM;
  307. } else {
  308. avctx->priv_data = NULL;
  309. }
  310. ret = avctx->codec->init(avctx);
  311. if (ret < 0) {
  312. av_freep(&avctx->priv_data);
  313. return ret;
  314. }
  315. return 0;
  316. }
  317. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  318. const short *samples)
  319. {
  320. int ret;
  321. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  322. avctx->frame_number++;
  323. return ret;
  324. }
  325. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  326. const AVFrame *pict)
  327. {
  328. int ret;
  329. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  330. emms_c(); //needed to avoid a emms_c() call before every return;
  331. avctx->frame_number++;
  332. return ret;
  333. }
  334. /**
  335. * decode a frame.
  336. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  337. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  338. * @param buf_size the size of the buffer in bytes
  339. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  340. * @return -1 if error, otherwise return the number of
  341. * bytes used.
  342. */
  343. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  344. int *got_picture_ptr,
  345. uint8_t *buf, int buf_size)
  346. {
  347. int ret;
  348. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  349. buf, buf_size);
  350. emms_c(); //needed to avoid a emms_c() call before every return;
  351. if (*got_picture_ptr)
  352. avctx->frame_number++;
  353. return ret;
  354. }
  355. /* decode an audio frame. return -1 if error, otherwise return the
  356. *number of bytes used. If no frame could be decompressed,
  357. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  358. *size in BYTES. */
  359. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  360. int *frame_size_ptr,
  361. uint8_t *buf, int buf_size)
  362. {
  363. int ret;
  364. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  365. buf, buf_size);
  366. avctx->frame_number++;
  367. return ret;
  368. }
  369. int avcodec_close(AVCodecContext *avctx)
  370. {
  371. if (avctx->codec->close)
  372. avctx->codec->close(avctx);
  373. av_freep(&avctx->priv_data);
  374. avctx->codec = NULL;
  375. return 0;
  376. }
  377. AVCodec *avcodec_find_encoder(enum CodecID id)
  378. {
  379. AVCodec *p;
  380. p = first_avcodec;
  381. while (p) {
  382. if (p->encode != NULL && p->id == id)
  383. return p;
  384. p = p->next;
  385. }
  386. return NULL;
  387. }
  388. AVCodec *avcodec_find_encoder_by_name(const char *name)
  389. {
  390. AVCodec *p;
  391. p = first_avcodec;
  392. while (p) {
  393. if (p->encode != NULL && strcmp(name,p->name) == 0)
  394. return p;
  395. p = p->next;
  396. }
  397. return NULL;
  398. }
  399. AVCodec *avcodec_find_decoder(enum CodecID id)
  400. {
  401. AVCodec *p;
  402. p = first_avcodec;
  403. while (p) {
  404. if (p->decode != NULL && p->id == id)
  405. return p;
  406. p = p->next;
  407. }
  408. return NULL;
  409. }
  410. AVCodec *avcodec_find_decoder_by_name(const char *name)
  411. {
  412. AVCodec *p;
  413. p = first_avcodec;
  414. while (p) {
  415. if (p->decode != NULL && strcmp(name,p->name) == 0)
  416. return p;
  417. p = p->next;
  418. }
  419. return NULL;
  420. }
  421. AVCodec *avcodec_find(enum CodecID id)
  422. {
  423. AVCodec *p;
  424. p = first_avcodec;
  425. while (p) {
  426. if (p->id == id)
  427. return p;
  428. p = p->next;
  429. }
  430. return NULL;
  431. }
  432. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  433. {
  434. const char *codec_name;
  435. AVCodec *p;
  436. char buf1[32];
  437. char channels_str[100];
  438. int bitrate;
  439. if (encode)
  440. p = avcodec_find_encoder(enc->codec_id);
  441. else
  442. p = avcodec_find_decoder(enc->codec_id);
  443. if (p) {
  444. codec_name = p->name;
  445. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  446. if (enc->sub_id == 2)
  447. codec_name = "mp2";
  448. else if (enc->sub_id == 1)
  449. codec_name = "mp1";
  450. }
  451. } else if (enc->codec_name[0] != '\0') {
  452. codec_name = enc->codec_name;
  453. } else {
  454. /* output avi tags */
  455. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  456. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  457. enc->codec_tag & 0xff,
  458. (enc->codec_tag >> 8) & 0xff,
  459. (enc->codec_tag >> 16) & 0xff,
  460. (enc->codec_tag >> 24) & 0xff);
  461. } else {
  462. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  463. }
  464. codec_name = buf1;
  465. }
  466. switch(enc->codec_type) {
  467. case CODEC_TYPE_VIDEO:
  468. snprintf(buf, buf_size,
  469. "Video: %s%s",
  470. codec_name, enc->mb_decision ? " (hq)" : "");
  471. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  472. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  473. ", %s",
  474. avcodec_get_pix_fmt_name(enc->pix_fmt));
  475. }
  476. if (enc->width) {
  477. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  478. ", %dx%d, %0.2f fps",
  479. enc->width, enc->height,
  480. (float)enc->frame_rate / enc->frame_rate_base);
  481. }
  482. if (encode) {
  483. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  484. ", q=%d-%d", enc->qmin, enc->qmax);
  485. }
  486. bitrate = enc->bit_rate;
  487. break;
  488. case CODEC_TYPE_AUDIO:
  489. snprintf(buf, buf_size,
  490. "Audio: %s",
  491. codec_name);
  492. switch (enc->channels) {
  493. case 1:
  494. strcpy(channels_str, "mono");
  495. break;
  496. case 2:
  497. strcpy(channels_str, "stereo");
  498. break;
  499. case 6:
  500. strcpy(channels_str, "5:1");
  501. break;
  502. default:
  503. sprintf(channels_str, "%d channels", enc->channels);
  504. break;
  505. }
  506. if (enc->sample_rate) {
  507. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  508. ", %d Hz, %s",
  509. enc->sample_rate,
  510. channels_str);
  511. }
  512. /* for PCM codecs, compute bitrate directly */
  513. switch(enc->codec_id) {
  514. case CODEC_ID_PCM_S16LE:
  515. case CODEC_ID_PCM_S16BE:
  516. case CODEC_ID_PCM_U16LE:
  517. case CODEC_ID_PCM_U16BE:
  518. bitrate = enc->sample_rate * enc->channels * 16;
  519. break;
  520. case CODEC_ID_PCM_S8:
  521. case CODEC_ID_PCM_U8:
  522. case CODEC_ID_PCM_ALAW:
  523. case CODEC_ID_PCM_MULAW:
  524. bitrate = enc->sample_rate * enc->channels * 8;
  525. break;
  526. default:
  527. bitrate = enc->bit_rate;
  528. break;
  529. }
  530. break;
  531. default:
  532. av_abort();
  533. }
  534. if (encode) {
  535. if (enc->flags & CODEC_FLAG_PASS1)
  536. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  537. ", pass 1");
  538. if (enc->flags & CODEC_FLAG_PASS2)
  539. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  540. ", pass 2");
  541. }
  542. if (bitrate != 0) {
  543. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  544. ", %d kb/s", bitrate / 1000);
  545. }
  546. }
  547. unsigned avcodec_version( void )
  548. {
  549. return LIBAVCODEC_VERSION_INT;
  550. }
  551. unsigned avcodec_build( void )
  552. {
  553. return LIBAVCODEC_BUILD;
  554. }
  555. /* must be called before any other functions */
  556. void avcodec_init(void)
  557. {
  558. static int inited = 0;
  559. if (inited != 0)
  560. return;
  561. inited = 1;
  562. dsputil_static_init();
  563. }
  564. /**
  565. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  566. */
  567. void avcodec_flush_buffers(AVCodecContext *avctx)
  568. {
  569. if(avctx->codec->flush)
  570. avctx->codec->flush(avctx);
  571. }
  572. void avcodec_default_free_buffers(AVCodecContext *s){
  573. int i, j;
  574. if(s->internal_buffer==NULL) return;
  575. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  576. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  577. for(j=0; j<4; j++){
  578. av_freep(&buf->base[j]);
  579. buf->data[j]= NULL;
  580. }
  581. }
  582. av_freep(&s->internal_buffer);
  583. s->internal_buffer_count=0;
  584. }
  585. char av_get_pict_type_char(int pict_type){
  586. switch(pict_type){
  587. case I_TYPE: return 'I';
  588. case P_TYPE: return 'P';
  589. case B_TYPE: return 'B';
  590. case S_TYPE: return 'S';
  591. case SI_TYPE:return 'i';
  592. case SP_TYPE:return 'p';
  593. default: return '?';
  594. }
  595. }
  596. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  597. int exact=1, sign=0;
  598. int64_t gcd, larger;
  599. assert(den != 0);
  600. if(den < 0){
  601. den= -den;
  602. nom= -nom;
  603. }
  604. if(nom < 0){
  605. nom= -nom;
  606. sign= 1;
  607. }
  608. for(;;){ //note is executed 1 or 2 times
  609. gcd = ff_gcd(nom, den);
  610. nom /= gcd;
  611. den /= gcd;
  612. larger= FFMAX(nom, den);
  613. if(larger > max){
  614. int64_t div= (larger + max - 1) / max;
  615. nom = (nom + div/2)/div;
  616. den = (den + div/2)/div;
  617. exact=0;
  618. }else
  619. break;
  620. }
  621. if(sign) nom= -nom;
  622. *dst_nom = nom;
  623. *dst_den = den;
  624. return exact;
  625. }
  626. int64_t av_rescale(int64_t a, int b, int c){
  627. uint64_t h, l;
  628. assert(c > 0);
  629. assert(b >=0);
  630. if(a<0) return -av_rescale(-a, b, c);
  631. h= a>>32;
  632. if(h==0) return a*b/c;
  633. l= a&0xFFFFFFFF;
  634. l *= b;
  635. h *= b;
  636. l += (h%c)<<32;
  637. return ((h/c)<<32) + l/c;
  638. }