mjpeg_parser.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * MJPEG parser
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. * Copyright (c) 2003-2004 Michael Niedermayer
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file libavcodec/mjpeg_parser.c
  25. * MJPEG parser.
  26. */
  27. #include "parser.h"
  28. /**
  29. * finds the end of the current frame in the bitstream.
  30. * @return the position of the first byte of the next frame, or -1
  31. */
  32. static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
  33. int vop_found, i;
  34. uint16_t state;
  35. vop_found= pc->frame_start_found;
  36. state= pc->state;
  37. i=0;
  38. if(!vop_found){
  39. for(i=0; i<buf_size; i++){
  40. state= (state<<8) | buf[i];
  41. if(state == 0xFFD8){
  42. i++;
  43. vop_found=1;
  44. break;
  45. }
  46. }
  47. }
  48. if(vop_found){
  49. /* EOF considered as end of frame */
  50. if (buf_size == 0)
  51. return 0;
  52. for(; i<buf_size; i++){
  53. state= (state<<8) | buf[i];
  54. if(state == 0xFFD8){
  55. pc->frame_start_found=0;
  56. pc->state=0;
  57. return i-1;
  58. }
  59. }
  60. }
  61. pc->frame_start_found= vop_found;
  62. pc->state= state;
  63. return END_NOT_FOUND;
  64. }
  65. static int jpeg_parse(AVCodecParserContext *s,
  66. AVCodecContext *avctx,
  67. const uint8_t **poutbuf, int *poutbuf_size,
  68. const uint8_t *buf, int buf_size)
  69. {
  70. ParseContext *pc = s->priv_data;
  71. int next;
  72. next= find_frame_end(pc, buf, buf_size);
  73. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  74. *poutbuf = NULL;
  75. *poutbuf_size = 0;
  76. return buf_size;
  77. }
  78. *poutbuf = buf;
  79. *poutbuf_size = buf_size;
  80. return next;
  81. }
  82. AVCodecParser mjpeg_parser = {
  83. { CODEC_ID_MJPEG },
  84. sizeof(ParseContext),
  85. NULL,
  86. jpeg_parse,
  87. ff_parse_close,
  88. };