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.

755 lines
19KB

  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->sample_aspect_ratio= (AVRational){0,1};
  275. s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
  276. s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
  277. }
  278. /**
  279. * allocates a AVCodecContext and set it to defaults.
  280. * this can be deallocated by simply calling free()
  281. */
  282. AVCodecContext *avcodec_alloc_context(void){
  283. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  284. if(avctx==NULL) return NULL;
  285. avcodec_get_context_defaults(avctx);
  286. return avctx;
  287. }
  288. /**
  289. * allocates a AVPFrame and set it to defaults.
  290. * this can be deallocated by simply calling free()
  291. */
  292. AVFrame *avcodec_alloc_frame(void){
  293. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  294. return pic;
  295. }
  296. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  297. {
  298. int ret;
  299. if(avctx->codec)
  300. return -1;
  301. avctx->codec = codec;
  302. avctx->codec_id = codec->id;
  303. avctx->frame_number = 0;
  304. if (codec->priv_data_size > 0) {
  305. avctx->priv_data = av_mallocz(codec->priv_data_size);
  306. if (!avctx->priv_data)
  307. return -ENOMEM;
  308. } else {
  309. avctx->priv_data = NULL;
  310. }
  311. ret = avctx->codec->init(avctx);
  312. if (ret < 0) {
  313. av_freep(&avctx->priv_data);
  314. return ret;
  315. }
  316. return 0;
  317. }
  318. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  319. const short *samples)
  320. {
  321. int ret;
  322. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  323. avctx->frame_number++;
  324. return ret;
  325. }
  326. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  327. const AVFrame *pict)
  328. {
  329. int ret;
  330. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  331. emms_c(); //needed to avoid a emms_c() call before every return;
  332. avctx->frame_number++;
  333. return ret;
  334. }
  335. /**
  336. * decode a frame.
  337. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
  338. * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
  339. * @param buf_size the size of the buffer in bytes
  340. * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
  341. * @return -1 if error, otherwise return the number of
  342. * bytes used.
  343. */
  344. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  345. int *got_picture_ptr,
  346. uint8_t *buf, int buf_size)
  347. {
  348. int ret;
  349. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  350. buf, buf_size);
  351. emms_c(); //needed to avoid a emms_c() call before every return;
  352. if (*got_picture_ptr)
  353. avctx->frame_number++;
  354. return ret;
  355. }
  356. /* decode an audio frame. return -1 if error, otherwise return the
  357. *number of bytes used. If no frame could be decompressed,
  358. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  359. *size in BYTES. */
  360. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  361. int *frame_size_ptr,
  362. uint8_t *buf, int buf_size)
  363. {
  364. int ret;
  365. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  366. buf, buf_size);
  367. avctx->frame_number++;
  368. return ret;
  369. }
  370. int avcodec_close(AVCodecContext *avctx)
  371. {
  372. if (avctx->codec->close)
  373. avctx->codec->close(avctx);
  374. av_freep(&avctx->priv_data);
  375. avctx->codec = NULL;
  376. return 0;
  377. }
  378. AVCodec *avcodec_find_encoder(enum CodecID id)
  379. {
  380. AVCodec *p;
  381. p = first_avcodec;
  382. while (p) {
  383. if (p->encode != NULL && p->id == id)
  384. return p;
  385. p = p->next;
  386. }
  387. return NULL;
  388. }
  389. AVCodec *avcodec_find_encoder_by_name(const char *name)
  390. {
  391. AVCodec *p;
  392. p = first_avcodec;
  393. while (p) {
  394. if (p->encode != NULL && strcmp(name,p->name) == 0)
  395. return p;
  396. p = p->next;
  397. }
  398. return NULL;
  399. }
  400. AVCodec *avcodec_find_decoder(enum CodecID id)
  401. {
  402. AVCodec *p;
  403. p = first_avcodec;
  404. while (p) {
  405. if (p->decode != NULL && p->id == id)
  406. return p;
  407. p = p->next;
  408. }
  409. return NULL;
  410. }
  411. AVCodec *avcodec_find_decoder_by_name(const char *name)
  412. {
  413. AVCodec *p;
  414. p = first_avcodec;
  415. while (p) {
  416. if (p->decode != NULL && strcmp(name,p->name) == 0)
  417. return p;
  418. p = p->next;
  419. }
  420. return NULL;
  421. }
  422. AVCodec *avcodec_find(enum CodecID id)
  423. {
  424. AVCodec *p;
  425. p = first_avcodec;
  426. while (p) {
  427. if (p->id == id)
  428. return p;
  429. p = p->next;
  430. }
  431. return NULL;
  432. }
  433. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  434. {
  435. const char *codec_name;
  436. AVCodec *p;
  437. char buf1[32];
  438. char channels_str[100];
  439. int bitrate;
  440. if (encode)
  441. p = avcodec_find_encoder(enc->codec_id);
  442. else
  443. p = avcodec_find_decoder(enc->codec_id);
  444. if (p) {
  445. codec_name = p->name;
  446. if (!encode && enc->codec_id == CODEC_ID_MP3) {
  447. if (enc->sub_id == 2)
  448. codec_name = "mp2";
  449. else if (enc->sub_id == 1)
  450. codec_name = "mp1";
  451. }
  452. } else if (enc->codec_name[0] != '\0') {
  453. codec_name = enc->codec_name;
  454. } else {
  455. /* output avi tags */
  456. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  457. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  458. enc->codec_tag & 0xff,
  459. (enc->codec_tag >> 8) & 0xff,
  460. (enc->codec_tag >> 16) & 0xff,
  461. (enc->codec_tag >> 24) & 0xff);
  462. } else {
  463. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  464. }
  465. codec_name = buf1;
  466. }
  467. switch(enc->codec_type) {
  468. case CODEC_TYPE_VIDEO:
  469. snprintf(buf, buf_size,
  470. "Video: %s%s",
  471. codec_name, enc->mb_decision ? " (hq)" : "");
  472. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  473. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  474. ", %s",
  475. avcodec_get_pix_fmt_name(enc->pix_fmt));
  476. }
  477. if (enc->width) {
  478. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  479. ", %dx%d, %0.2f fps",
  480. enc->width, enc->height,
  481. (float)enc->frame_rate / enc->frame_rate_base);
  482. }
  483. if (encode) {
  484. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  485. ", q=%d-%d", enc->qmin, enc->qmax);
  486. }
  487. bitrate = enc->bit_rate;
  488. break;
  489. case CODEC_TYPE_AUDIO:
  490. snprintf(buf, buf_size,
  491. "Audio: %s",
  492. codec_name);
  493. switch (enc->channels) {
  494. case 1:
  495. strcpy(channels_str, "mono");
  496. break;
  497. case 2:
  498. strcpy(channels_str, "stereo");
  499. break;
  500. case 6:
  501. strcpy(channels_str, "5:1");
  502. break;
  503. default:
  504. sprintf(channels_str, "%d channels", enc->channels);
  505. break;
  506. }
  507. if (enc->sample_rate) {
  508. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  509. ", %d Hz, %s",
  510. enc->sample_rate,
  511. channels_str);
  512. }
  513. /* for PCM codecs, compute bitrate directly */
  514. switch(enc->codec_id) {
  515. case CODEC_ID_PCM_S16LE:
  516. case CODEC_ID_PCM_S16BE:
  517. case CODEC_ID_PCM_U16LE:
  518. case CODEC_ID_PCM_U16BE:
  519. bitrate = enc->sample_rate * enc->channels * 16;
  520. break;
  521. case CODEC_ID_PCM_S8:
  522. case CODEC_ID_PCM_U8:
  523. case CODEC_ID_PCM_ALAW:
  524. case CODEC_ID_PCM_MULAW:
  525. bitrate = enc->sample_rate * enc->channels * 8;
  526. break;
  527. default:
  528. bitrate = enc->bit_rate;
  529. break;
  530. }
  531. break;
  532. default:
  533. av_abort();
  534. }
  535. if (encode) {
  536. if (enc->flags & CODEC_FLAG_PASS1)
  537. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  538. ", pass 1");
  539. if (enc->flags & CODEC_FLAG_PASS2)
  540. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  541. ", pass 2");
  542. }
  543. if (bitrate != 0) {
  544. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  545. ", %d kb/s", bitrate / 1000);
  546. }
  547. }
  548. unsigned avcodec_version( void )
  549. {
  550. return LIBAVCODEC_VERSION_INT;
  551. }
  552. unsigned avcodec_build( void )
  553. {
  554. return LIBAVCODEC_BUILD;
  555. }
  556. /* must be called before any other functions */
  557. void avcodec_init(void)
  558. {
  559. static int inited = 0;
  560. if (inited != 0)
  561. return;
  562. inited = 1;
  563. dsputil_static_init();
  564. }
  565. /**
  566. * Flush buffers, should be called when seeking or when swicthing to a different stream.
  567. */
  568. void avcodec_flush_buffers(AVCodecContext *avctx)
  569. {
  570. if(avctx->codec->flush)
  571. avctx->codec->flush(avctx);
  572. }
  573. void avcodec_default_free_buffers(AVCodecContext *s){
  574. int i, j;
  575. if(s->internal_buffer==NULL) return;
  576. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  577. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  578. for(j=0; j<4; j++){
  579. av_freep(&buf->base[j]);
  580. buf->data[j]= NULL;
  581. }
  582. }
  583. av_freep(&s->internal_buffer);
  584. s->internal_buffer_count=0;
  585. }
  586. char av_get_pict_type_char(int pict_type){
  587. switch(pict_type){
  588. case I_TYPE: return 'I';
  589. case P_TYPE: return 'P';
  590. case B_TYPE: return 'B';
  591. case S_TYPE: return 'S';
  592. case SI_TYPE:return 'i';
  593. case SP_TYPE:return 'p';
  594. default: return '?';
  595. }
  596. }
  597. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  598. int exact=1, sign=0;
  599. int64_t gcd;
  600. assert(den != 0);
  601. if(den < 0){
  602. den= -den;
  603. nom= -nom;
  604. }
  605. if(nom < 0){
  606. nom= -nom;
  607. sign= 1;
  608. }
  609. gcd = ff_gcd(nom, den);
  610. nom /= gcd;
  611. den /= gcd;
  612. if(nom > max || den > max){
  613. AVRational a0={0,1}, a1={1,0};
  614. exact=0;
  615. for(;;){
  616. int64_t x= nom / den;
  617. int64_t a2n= x*a1.num + a0.num;
  618. int64_t a2d= x*a1.den + a0.den;
  619. if(a2n > max || a2d > max) break;
  620. nom %= den;
  621. a0= a1;
  622. a1= (AVRational){a2n, a2d};
  623. if(nom==0) break;
  624. x= nom; nom=den; den=x;
  625. }
  626. nom= a1.num;
  627. den= a1.den;
  628. }
  629. assert(ff_gcd(nom, den) == 1);
  630. if(sign) nom= -nom;
  631. *dst_nom = nom;
  632. *dst_den = den;
  633. return exact;
  634. }
  635. int64_t av_rescale(int64_t a, int b, int c){
  636. uint64_t h, l;
  637. assert(c > 0);
  638. assert(b >=0);
  639. if(a<0) return -av_rescale(-a, b, c);
  640. h= a>>32;
  641. if(h==0) return a*b/c;
  642. l= a&0xFFFFFFFF;
  643. l *= b;
  644. h *= b;
  645. l += (h%c)<<32;
  646. return ((h/c)<<32) + l/c;
  647. }