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.

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