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.

615 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. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n");
  26. ptr = av_malloc(size);
  27. if (!ptr)
  28. return NULL;
  29. memset(ptr, 0, size);
  30. return ptr;
  31. }
  32. char *av_strdup(const char *s)
  33. {
  34. char *ptr;
  35. int len;
  36. len = strlen(s) + 1;
  37. ptr = av_malloc(len);
  38. if (!ptr)
  39. return NULL;
  40. memcpy(ptr, s, len);
  41. return ptr;
  42. }
  43. /**
  44. * realloc which does nothing if the block is large enough
  45. */
  46. void *av_fast_realloc(void *ptr, int *size, int min_size)
  47. {
  48. if(min_size < *size)
  49. return ptr;
  50. *size= min_size + 10*1024;
  51. return av_realloc(ptr, *size);
  52. }
  53. /* allocation of static arrays - do not use for normal allocation */
  54. static unsigned int last_static = 0;
  55. static char*** array_static = NULL;
  56. static const unsigned int grow_static = 64; // ^2
  57. void *__av_mallocz_static(void** location, unsigned int size)
  58. {
  59. int l = (last_static + grow_static) & ~(grow_static - 1);
  60. void *ptr = av_mallocz(size);
  61. if (!ptr)
  62. return NULL;
  63. if (location)
  64. {
  65. if (l > last_static)
  66. array_static = av_realloc(array_static, l);
  67. array_static[last_static++] = (char**) location;
  68. *location = ptr;
  69. }
  70. return ptr;
  71. }
  72. /* free all static arrays and reset pointers to 0 */
  73. void av_free_static()
  74. {
  75. if (array_static)
  76. {
  77. unsigned i;
  78. for (i = 0; i < last_static; i++)
  79. {
  80. av_free(*array_static[i]);
  81. *array_static[i] = NULL;
  82. }
  83. av_free(array_static);
  84. array_static = 0;
  85. }
  86. last_static = 0;
  87. }
  88. /* cannot call it directly because of 'void **' casting is not automatic */
  89. void __av_freep(void **ptr)
  90. {
  91. av_free(*ptr);
  92. *ptr = NULL;
  93. }
  94. /* encoder management */
  95. AVCodec *first_avcodec;
  96. void register_avcodec(AVCodec *format)
  97. {
  98. AVCodec **p;
  99. p = &first_avcodec;
  100. while (*p != NULL) p = &(*p)->next;
  101. *p = format;
  102. format->next = NULL;
  103. }
  104. typedef struct DefaultPicOpaque{
  105. int last_pic_num;
  106. uint8_t *data[4];
  107. }DefaultPicOpaque;
  108. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  109. int i;
  110. const int width = s->width;
  111. const int height= s->height;
  112. DefaultPicOpaque *opaque;
  113. assert(pic->data[0]==NULL);
  114. assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
  115. if(pic->opaque){
  116. opaque= (DefaultPicOpaque *)pic->opaque;
  117. for(i=0; i<3; i++)
  118. pic->data[i]= opaque->data[i];
  119. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  120. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  121. opaque->last_pic_num= pic->coded_picture_number;
  122. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  123. }else{
  124. int align, h_chroma_shift, v_chroma_shift;
  125. int w, h, pixel_size;
  126. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  127. switch(s->pix_fmt){
  128. case PIX_FMT_YUV422:
  129. pixel_size=2;
  130. break;
  131. case PIX_FMT_RGB24:
  132. case PIX_FMT_BGR24:
  133. pixel_size=3;
  134. break;
  135. case PIX_FMT_RGBA32:
  136. pixel_size=4;
  137. break;
  138. default:
  139. pixel_size=1;
  140. }
  141. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  142. else align=15;
  143. w= (width +align)&~align;
  144. h= (height+align)&~align;
  145. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  146. w+= EDGE_WIDTH*2;
  147. h+= EDGE_WIDTH*2;
  148. }
  149. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  150. if(opaque==NULL) return -1;
  151. pic->opaque= opaque;
  152. opaque->last_pic_num= -256*256*256*64;
  153. for(i=0; i<3; i++){
  154. int h_shift= i==0 ? 0 : h_chroma_shift;
  155. int v_shift= i==0 ? 0 : v_chroma_shift;
  156. pic->linesize[i]= pixel_size*w>>h_shift;
  157. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  158. if(pic->base[i]==NULL) return -1;
  159. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  160. if(s->flags&CODEC_FLAG_EMU_EDGE)
  161. pic->data[i] = pic->base[i] + 16; //FIXME 16
  162. else
  163. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
  164. opaque->data[i]= pic->data[i];
  165. }
  166. pic->age= 256*256*256*64;
  167. pic->type= FF_BUFFER_TYPE_INTERNAL;
  168. }
  169. return 0;
  170. }
  171. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  172. int i;
  173. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  174. for(i=0; i<3; i++)
  175. pic->data[i]=NULL;
  176. //printf("R%X\n", pic->opaque);
  177. }
  178. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
  179. return fmt[0];
  180. }
  181. void avcodec_get_context_defaults(AVCodecContext *s){
  182. s->bit_rate= 800*1000;
  183. s->bit_rate_tolerance= s->bit_rate*10;
  184. s->qmin= 2;
  185. s->qmax= 31;
  186. s->mb_qmin= 2;
  187. s->mb_qmax= 31;
  188. s->rc_eq= "tex^qComp";
  189. s->qcompress= 0.5;
  190. s->max_qdiff= 3;
  191. s->b_quant_factor=1.25;
  192. s->b_quant_offset=1.25;
  193. s->i_quant_factor=-0.8;
  194. s->i_quant_offset=0.0;
  195. s->error_concealment= 3;
  196. s->error_resilience= 1;
  197. s->workaround_bugs= FF_BUG_AUTODETECT;
  198. s->frame_rate = 25 * FRAME_RATE_BASE;
  199. s->gop_size= 50;
  200. s->me_method= ME_EPZS;
  201. s->get_buffer= avcodec_default_get_buffer;
  202. s->release_buffer= avcodec_default_release_buffer;
  203. s->get_format= avcodec_default_get_format;
  204. s->me_subpel_quality=8;
  205. }
  206. /**
  207. * allocates a AVCodecContext and set it to defaults.
  208. * this can be deallocated by simply calling free()
  209. */
  210. AVCodecContext *avcodec_alloc_context(void){
  211. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  212. if(avctx==NULL) return NULL;
  213. avcodec_get_context_defaults(avctx);
  214. return avctx;
  215. }
  216. /**
  217. * allocates a AVPFrame and set it to defaults.
  218. * this can be deallocated by simply calling free()
  219. */
  220. AVFrame *avcodec_alloc_frame(void){
  221. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  222. return pic;
  223. }
  224. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  225. {
  226. int ret;
  227. avctx->codec = codec;
  228. avctx->codec_id = codec->id;
  229. avctx->frame_number = 0;
  230. if (codec->priv_data_size > 0) {
  231. avctx->priv_data = av_mallocz(codec->priv_data_size);
  232. if (!avctx->priv_data)
  233. return -ENOMEM;
  234. } else {
  235. avctx->priv_data = NULL;
  236. }
  237. ret = avctx->codec->init(avctx);
  238. if (ret < 0) {
  239. av_freep(&avctx->priv_data);
  240. return ret;
  241. }
  242. return 0;
  243. }
  244. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  245. const short *samples)
  246. {
  247. int ret;
  248. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  249. avctx->frame_number++;
  250. return ret;
  251. }
  252. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  253. const AVFrame *pict)
  254. {
  255. int ret;
  256. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  257. emms_c(); //needed to avoid a emms_c() call before every return;
  258. avctx->frame_number++;
  259. return ret;
  260. }
  261. /* decode a frame. return -1 if error, otherwise return the number of
  262. bytes used. If no frame could be decompressed, *got_picture_ptr is
  263. zero. Otherwise, it is non zero */
  264. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  265. int *got_picture_ptr,
  266. UINT8 *buf, int buf_size)
  267. {
  268. int ret;
  269. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  270. buf, buf_size);
  271. emms_c(); //needed to avoid a emms_c() call before every return;
  272. if (*got_picture_ptr)
  273. avctx->frame_number++;
  274. return ret;
  275. }
  276. /* decode an audio frame. return -1 if error, otherwise return the
  277. *number of bytes used. If no frame could be decompressed,
  278. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  279. *size in BYTES. */
  280. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  281. int *frame_size_ptr,
  282. UINT8 *buf, int buf_size)
  283. {
  284. int ret;
  285. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  286. buf, buf_size);
  287. avctx->frame_number++;
  288. return ret;
  289. }
  290. int avcodec_close(AVCodecContext *avctx)
  291. {
  292. if (avctx->codec->close)
  293. avctx->codec->close(avctx);
  294. av_freep(&avctx->priv_data);
  295. avctx->codec = NULL;
  296. return 0;
  297. }
  298. AVCodec *avcodec_find_encoder(enum CodecID id)
  299. {
  300. AVCodec *p;
  301. p = first_avcodec;
  302. while (p) {
  303. if (p->encode != NULL && p->id == id)
  304. return p;
  305. p = p->next;
  306. }
  307. return NULL;
  308. }
  309. AVCodec *avcodec_find_encoder_by_name(const char *name)
  310. {
  311. AVCodec *p;
  312. p = first_avcodec;
  313. while (p) {
  314. if (p->encode != NULL && strcmp(name,p->name) == 0)
  315. return p;
  316. p = p->next;
  317. }
  318. return NULL;
  319. }
  320. AVCodec *avcodec_find_decoder(enum CodecID id)
  321. {
  322. AVCodec *p;
  323. p = first_avcodec;
  324. while (p) {
  325. if (p->decode != NULL && p->id == id)
  326. return p;
  327. p = p->next;
  328. }
  329. return NULL;
  330. }
  331. AVCodec *avcodec_find_decoder_by_name(const char *name)
  332. {
  333. AVCodec *p;
  334. p = first_avcodec;
  335. while (p) {
  336. if (p->decode != NULL && strcmp(name,p->name) == 0)
  337. return p;
  338. p = p->next;
  339. }
  340. return NULL;
  341. }
  342. AVCodec *avcodec_find(enum CodecID id)
  343. {
  344. AVCodec *p;
  345. p = first_avcodec;
  346. while (p) {
  347. if (p->id == id)
  348. return p;
  349. p = p->next;
  350. }
  351. return NULL;
  352. }
  353. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  354. {
  355. const char *codec_name;
  356. AVCodec *p;
  357. char buf1[32];
  358. char channels_str[100];
  359. int bitrate;
  360. if (encode)
  361. p = avcodec_find_encoder(enc->codec_id);
  362. else
  363. p = avcodec_find_decoder(enc->codec_id);
  364. if (p) {
  365. codec_name = p->name;
  366. } else if (enc->codec_name[0] != '\0') {
  367. codec_name = enc->codec_name;
  368. } else {
  369. /* output avi tags */
  370. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  371. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  372. enc->codec_tag & 0xff,
  373. (enc->codec_tag >> 8) & 0xff,
  374. (enc->codec_tag >> 16) & 0xff,
  375. (enc->codec_tag >> 24) & 0xff);
  376. } else {
  377. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  378. }
  379. codec_name = buf1;
  380. }
  381. switch(enc->codec_type) {
  382. case CODEC_TYPE_VIDEO:
  383. snprintf(buf, buf_size,
  384. "Video: %s%s",
  385. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  386. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  387. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  388. ", %s",
  389. avcodec_get_pix_fmt_name(enc->pix_fmt));
  390. }
  391. if (enc->width) {
  392. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  393. ", %dx%d, %0.2f fps",
  394. enc->width, enc->height,
  395. (float)enc->frame_rate / FRAME_RATE_BASE);
  396. }
  397. if (encode) {
  398. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  399. ", q=%d-%d", enc->qmin, enc->qmax);
  400. }
  401. bitrate = enc->bit_rate;
  402. break;
  403. case CODEC_TYPE_AUDIO:
  404. snprintf(buf, buf_size,
  405. "Audio: %s",
  406. codec_name);
  407. switch (enc->channels) {
  408. case 1:
  409. strcpy(channels_str, "mono");
  410. break;
  411. case 2:
  412. strcpy(channels_str, "stereo");
  413. break;
  414. case 6:
  415. strcpy(channels_str, "5:1");
  416. break;
  417. default:
  418. sprintf(channels_str, "%d channels", enc->channels);
  419. break;
  420. }
  421. if (enc->sample_rate) {
  422. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  423. ", %d Hz, %s",
  424. enc->sample_rate,
  425. channels_str);
  426. }
  427. /* for PCM codecs, compute bitrate directly */
  428. switch(enc->codec_id) {
  429. case CODEC_ID_PCM_S16LE:
  430. case CODEC_ID_PCM_S16BE:
  431. case CODEC_ID_PCM_U16LE:
  432. case CODEC_ID_PCM_U16BE:
  433. bitrate = enc->sample_rate * enc->channels * 16;
  434. break;
  435. case CODEC_ID_PCM_S8:
  436. case CODEC_ID_PCM_U8:
  437. case CODEC_ID_PCM_ALAW:
  438. case CODEC_ID_PCM_MULAW:
  439. bitrate = enc->sample_rate * enc->channels * 8;
  440. break;
  441. default:
  442. bitrate = enc->bit_rate;
  443. break;
  444. }
  445. break;
  446. default:
  447. av_abort();
  448. }
  449. if (encode) {
  450. if (enc->flags & CODEC_FLAG_PASS1)
  451. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  452. ", pass 1");
  453. if (enc->flags & CODEC_FLAG_PASS2)
  454. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  455. ", pass 2");
  456. }
  457. if (bitrate != 0) {
  458. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  459. ", %d kb/s", bitrate / 1000);
  460. }
  461. }
  462. unsigned avcodec_version( void )
  463. {
  464. return LIBAVCODEC_VERSION_INT;
  465. }
  466. unsigned avcodec_build( void )
  467. {
  468. return LIBAVCODEC_BUILD;
  469. }
  470. /* must be called before any other functions */
  471. void avcodec_init(void)
  472. {
  473. static int inited = 0;
  474. if (inited != 0)
  475. return;
  476. inited = 1;
  477. //dsputil_init();
  478. }
  479. /* this can be called after seeking and before trying to decode the next keyframe */
  480. void avcodec_flush_buffers(AVCodecContext *avctx)
  481. {
  482. int i;
  483. MpegEncContext *s = avctx->priv_data;
  484. switch(avctx->codec_id){
  485. case CODEC_ID_MPEG1VIDEO:
  486. case CODEC_ID_H263:
  487. case CODEC_ID_RV10:
  488. case CODEC_ID_MJPEG:
  489. case CODEC_ID_MJPEGB:
  490. case CODEC_ID_MPEG4:
  491. case CODEC_ID_MSMPEG4V1:
  492. case CODEC_ID_MSMPEG4V2:
  493. case CODEC_ID_MSMPEG4V3:
  494. case CODEC_ID_WMV1:
  495. case CODEC_ID_WMV2:
  496. case CODEC_ID_H263P:
  497. case CODEC_ID_H263I:
  498. case CODEC_ID_SVQ1:
  499. for(i=0; i<MAX_PICTURE_COUNT; i++){
  500. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  501. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  502. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  503. }
  504. s->last_picture.data[0] = s->next_picture.data[0] = NULL;
  505. break;
  506. default:
  507. //FIXME
  508. break;
  509. }
  510. }
  511. static int raw_encode_init(AVCodecContext *s)
  512. {
  513. return 0;
  514. }
  515. static int raw_decode_frame(AVCodecContext *avctx,
  516. void *data, int *data_size,
  517. UINT8 *buf, int buf_size)
  518. {
  519. return -1;
  520. }
  521. static int raw_encode_frame(AVCodecContext *avctx,
  522. unsigned char *frame, int buf_size, void *data)
  523. {
  524. return -1;
  525. }
  526. AVCodec rawvideo_codec = {
  527. "rawvideo",
  528. CODEC_TYPE_VIDEO,
  529. CODEC_ID_RAWVIDEO,
  530. 0,
  531. raw_encode_init,
  532. raw_encode_frame,
  533. NULL,
  534. raw_decode_frame,
  535. };