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.

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