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.

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