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.

679 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()
  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_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. int h_shift= i==0 ? 0 : h_chroma_shift;
  158. 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] + 16; //FIXME 16
  165. else
  166. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
  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. }
  210. /**
  211. * allocates a AVCodecContext and set it to defaults.
  212. * this can be deallocated by simply calling free()
  213. */
  214. AVCodecContext *avcodec_alloc_context(void){
  215. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  216. if(avctx==NULL) return NULL;
  217. avcodec_get_context_defaults(avctx);
  218. return avctx;
  219. }
  220. /**
  221. * allocates a AVPFrame and set it to defaults.
  222. * this can be deallocated by simply calling free()
  223. */
  224. AVFrame *avcodec_alloc_frame(void){
  225. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  226. return pic;
  227. }
  228. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  229. {
  230. int ret;
  231. avctx->codec = codec;
  232. avctx->codec_id = codec->id;
  233. avctx->frame_number = 0;
  234. if (codec->priv_data_size > 0) {
  235. avctx->priv_data = av_mallocz(codec->priv_data_size);
  236. if (!avctx->priv_data)
  237. return -ENOMEM;
  238. } else {
  239. avctx->priv_data = NULL;
  240. }
  241. ret = avctx->codec->init(avctx);
  242. if (ret < 0) {
  243. av_freep(&avctx->priv_data);
  244. return ret;
  245. }
  246. return 0;
  247. }
  248. int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  249. const short *samples)
  250. {
  251. int ret;
  252. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  253. avctx->frame_number++;
  254. return ret;
  255. }
  256. int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  257. const AVFrame *pict)
  258. {
  259. int ret;
  260. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  261. emms_c(); //needed to avoid a emms_c() call before every return;
  262. avctx->frame_number++;
  263. return ret;
  264. }
  265. /* decode a frame. return -1 if error, otherwise return the number of
  266. bytes used. If no frame could be decompressed, *got_picture_ptr is
  267. zero. Otherwise, it is non zero */
  268. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  269. int *got_picture_ptr,
  270. uint8_t *buf, int buf_size)
  271. {
  272. int ret;
  273. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  274. buf, buf_size);
  275. emms_c(); //needed to avoid a emms_c() call before every return;
  276. if (*got_picture_ptr)
  277. avctx->frame_number++;
  278. return ret;
  279. }
  280. /* decode an audio frame. return -1 if error, otherwise return the
  281. *number of bytes used. If no frame could be decompressed,
  282. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  283. *size in BYTES. */
  284. int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
  285. int *frame_size_ptr,
  286. uint8_t *buf, int buf_size)
  287. {
  288. int ret;
  289. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  290. buf, buf_size);
  291. avctx->frame_number++;
  292. return ret;
  293. }
  294. int avcodec_close(AVCodecContext *avctx)
  295. {
  296. if (avctx->codec->close)
  297. avctx->codec->close(avctx);
  298. av_freep(&avctx->priv_data);
  299. avctx->codec = NULL;
  300. return 0;
  301. }
  302. AVCodec *avcodec_find_encoder(enum CodecID id)
  303. {
  304. AVCodec *p;
  305. p = first_avcodec;
  306. while (p) {
  307. if (p->encode != NULL && p->id == id)
  308. return p;
  309. p = p->next;
  310. }
  311. return NULL;
  312. }
  313. AVCodec *avcodec_find_encoder_by_name(const char *name)
  314. {
  315. AVCodec *p;
  316. p = first_avcodec;
  317. while (p) {
  318. if (p->encode != NULL && strcmp(name,p->name) == 0)
  319. return p;
  320. p = p->next;
  321. }
  322. return NULL;
  323. }
  324. AVCodec *avcodec_find_decoder(enum CodecID id)
  325. {
  326. AVCodec *p;
  327. p = first_avcodec;
  328. while (p) {
  329. if (p->decode != NULL && p->id == id)
  330. return p;
  331. p = p->next;
  332. }
  333. return NULL;
  334. }
  335. AVCodec *avcodec_find_decoder_by_name(const char *name)
  336. {
  337. AVCodec *p;
  338. p = first_avcodec;
  339. while (p) {
  340. if (p->decode != NULL && strcmp(name,p->name) == 0)
  341. return p;
  342. p = p->next;
  343. }
  344. return NULL;
  345. }
  346. AVCodec *avcodec_find(enum CodecID id)
  347. {
  348. AVCodec *p;
  349. p = first_avcodec;
  350. while (p) {
  351. if (p->id == id)
  352. return p;
  353. p = p->next;
  354. }
  355. return NULL;
  356. }
  357. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  358. {
  359. const char *codec_name;
  360. AVCodec *p;
  361. char buf1[32];
  362. char channels_str[100];
  363. int bitrate;
  364. if (encode)
  365. p = avcodec_find_encoder(enc->codec_id);
  366. else
  367. p = avcodec_find_decoder(enc->codec_id);
  368. if (p) {
  369. codec_name = p->name;
  370. } else if (enc->codec_name[0] != '\0') {
  371. codec_name = enc->codec_name;
  372. } else {
  373. /* output avi tags */
  374. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  375. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  376. enc->codec_tag & 0xff,
  377. (enc->codec_tag >> 8) & 0xff,
  378. (enc->codec_tag >> 16) & 0xff,
  379. (enc->codec_tag >> 24) & 0xff);
  380. } else {
  381. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  382. }
  383. codec_name = buf1;
  384. }
  385. switch(enc->codec_type) {
  386. case CODEC_TYPE_VIDEO:
  387. snprintf(buf, buf_size,
  388. "Video: %s%s",
  389. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  390. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  391. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  392. ", %s",
  393. avcodec_get_pix_fmt_name(enc->pix_fmt));
  394. }
  395. if (enc->width) {
  396. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  397. ", %dx%d, %0.2f fps",
  398. enc->width, enc->height,
  399. (float)enc->frame_rate / enc->frame_rate_base);
  400. }
  401. if (encode) {
  402. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  403. ", q=%d-%d", enc->qmin, enc->qmax);
  404. }
  405. bitrate = enc->bit_rate;
  406. break;
  407. case CODEC_TYPE_AUDIO:
  408. snprintf(buf, buf_size,
  409. "Audio: %s",
  410. codec_name);
  411. switch (enc->channels) {
  412. case 1:
  413. strcpy(channels_str, "mono");
  414. break;
  415. case 2:
  416. strcpy(channels_str, "stereo");
  417. break;
  418. case 6:
  419. strcpy(channels_str, "5:1");
  420. break;
  421. default:
  422. sprintf(channels_str, "%d channels", enc->channels);
  423. break;
  424. }
  425. if (enc->sample_rate) {
  426. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  427. ", %d Hz, %s",
  428. enc->sample_rate,
  429. channels_str);
  430. }
  431. /* for PCM codecs, compute bitrate directly */
  432. switch(enc->codec_id) {
  433. case CODEC_ID_PCM_S16LE:
  434. case CODEC_ID_PCM_S16BE:
  435. case CODEC_ID_PCM_U16LE:
  436. case CODEC_ID_PCM_U16BE:
  437. bitrate = enc->sample_rate * enc->channels * 16;
  438. break;
  439. case CODEC_ID_PCM_S8:
  440. case CODEC_ID_PCM_U8:
  441. case CODEC_ID_PCM_ALAW:
  442. case CODEC_ID_PCM_MULAW:
  443. bitrate = enc->sample_rate * enc->channels * 8;
  444. break;
  445. default:
  446. bitrate = enc->bit_rate;
  447. break;
  448. }
  449. break;
  450. default:
  451. av_abort();
  452. }
  453. if (encode) {
  454. if (enc->flags & CODEC_FLAG_PASS1)
  455. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  456. ", pass 1");
  457. if (enc->flags & CODEC_FLAG_PASS2)
  458. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  459. ", pass 2");
  460. }
  461. if (bitrate != 0) {
  462. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  463. ", %d kb/s", bitrate / 1000);
  464. }
  465. }
  466. unsigned avcodec_version( void )
  467. {
  468. return LIBAVCODEC_VERSION_INT;
  469. }
  470. unsigned avcodec_build( void )
  471. {
  472. return LIBAVCODEC_BUILD;
  473. }
  474. /* must be called before any other functions */
  475. void avcodec_init(void)
  476. {
  477. static int inited = 0;
  478. if (inited != 0)
  479. return;
  480. inited = 1;
  481. //dsputil_init();
  482. }
  483. /* this can be called after seeking and before trying to decode the next keyframe */
  484. void avcodec_flush_buffers(AVCodecContext *avctx)
  485. {
  486. int i;
  487. MpegEncContext *s = avctx->priv_data;
  488. switch(avctx->codec_id){
  489. case CODEC_ID_MPEG1VIDEO:
  490. case CODEC_ID_H263:
  491. case CODEC_ID_RV10:
  492. case CODEC_ID_MJPEG:
  493. case CODEC_ID_MJPEGB:
  494. case CODEC_ID_MPEG4:
  495. case CODEC_ID_MSMPEG4V1:
  496. case CODEC_ID_MSMPEG4V2:
  497. case CODEC_ID_MSMPEG4V3:
  498. case CODEC_ID_WMV1:
  499. case CODEC_ID_WMV2:
  500. case CODEC_ID_H263P:
  501. case CODEC_ID_H263I:
  502. case CODEC_ID_SVQ1:
  503. for(i=0; i<MAX_PICTURE_COUNT; i++){
  504. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  505. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  506. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  507. }
  508. s->last_picture.data[0] = s->next_picture.data[0] = NULL;
  509. break;
  510. default:
  511. //FIXME
  512. break;
  513. }
  514. }
  515. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  516. int exact=1, sign=0;
  517. int64_t gcd, larger;
  518. assert(den != 0);
  519. if(den < 0){
  520. den= -den;
  521. nom= -nom;
  522. }
  523. if(nom < 0){
  524. nom= -nom;
  525. sign= 1;
  526. }
  527. for(;;){ //note is executed 1 or 2 times
  528. gcd = ff_gcd(nom, den);
  529. nom /= gcd;
  530. den /= gcd;
  531. larger= FFMAX(nom, den);
  532. if(larger > max){
  533. int64_t div= (larger + max - 1) / max;
  534. nom = (nom + div/2)/div;
  535. den = (den + div/2)/div;
  536. exact=0;
  537. }else
  538. break;
  539. }
  540. if(sign) nom= -nom;
  541. *dst_nom = nom;
  542. *dst_den = den;
  543. return exact;
  544. }
  545. int64_t av_rescale(int64_t a, int b, int c){
  546. uint64_t h, l;
  547. assert(c > 0);
  548. assert(b >=0);
  549. if(a<0) return -av_rescale(-a, b, c);
  550. h= a>>32;
  551. if(h==0) return a*b/c;
  552. l= a&0xFFFFFFFF;
  553. l *= b;
  554. h *= b;
  555. l += (h%c)<<32;
  556. return ((h/c)<<32) + l/c;
  557. }
  558. static int raw_encode_init(AVCodecContext *s)
  559. {
  560. return 0;
  561. }
  562. static int raw_decode_frame(AVCodecContext *avctx,
  563. void *data, int *data_size,
  564. uint8_t *buf, int buf_size)
  565. {
  566. return -1;
  567. }
  568. static int raw_encode_frame(AVCodecContext *avctx,
  569. unsigned char *frame, int buf_size, void *data)
  570. {
  571. return -1;
  572. }
  573. AVCodec rawvideo_codec = {
  574. "rawvideo",
  575. CODEC_TYPE_VIDEO,
  576. CODEC_ID_RAWVIDEO,
  577. 0,
  578. raw_encode_init,
  579. raw_encode_frame,
  580. NULL,
  581. raw_decode_frame,
  582. };