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.

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