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.

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