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.

653 lines
16KB

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