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.

137 lines
3.9KB

  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
  25. * MJPEG parser.
  26. */
  27. #include "parser.h"
  28. typedef struct MJPEGParserContext{
  29. ParseContext pc;
  30. int size;
  31. }MJPEGParserContext;
  32. /**
  33. * Find the end of the current frame in the bitstream.
  34. * @return the position of the first byte of the next frame, or -1
  35. */
  36. static int find_frame_end(MJPEGParserContext *m, const uint8_t *buf, int buf_size){
  37. ParseContext *pc= &m->pc;
  38. int vop_found, i;
  39. uint32_t state;
  40. vop_found= pc->frame_start_found;
  41. state= pc->state;
  42. i=0;
  43. if(!vop_found){
  44. for(i=0; i<buf_size;){
  45. state= (state<<8) | buf[i];
  46. if(state>=0xFFC00000 && state<=0xFFFEFFFF){
  47. if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){
  48. i++;
  49. vop_found=1;
  50. break;
  51. }else if(state<0xFFD00000 || state>0xFFD9FFFF){
  52. m->size= (state&0xFFFF)-1;
  53. }
  54. }
  55. if(m->size>0){
  56. int size= FFMIN(buf_size-i, m->size);
  57. i+=size;
  58. m->size-=size;
  59. state=0;
  60. continue;
  61. }else
  62. i++;
  63. }
  64. }
  65. if(vop_found){
  66. /* EOF considered as end of frame */
  67. if (buf_size == 0)
  68. return 0;
  69. for(; i<buf_size;){
  70. state= (state<<8) | buf[i];
  71. if(state>=0xFFC00000 && state<=0xFFFEFFFF){
  72. if(state>=0xFFD8FFC0 && state<=0xFFD8FFFF){
  73. pc->frame_start_found=0;
  74. pc->state=0;
  75. return i-3;
  76. } else if(state<0xFFD00000 || state>0xFFD9FFFF){
  77. m->size= (state&0xFFFF)-1;
  78. if (m->size >= 0x8000)
  79. m->size = 0;
  80. }
  81. }
  82. if(m->size>0){
  83. int size= FFMIN(buf_size-i, m->size);
  84. i+=size;
  85. m->size-=size;
  86. state=0;
  87. continue;
  88. }else
  89. i++;
  90. }
  91. }
  92. pc->frame_start_found= vop_found;
  93. pc->state= state;
  94. return END_NOT_FOUND;
  95. }
  96. static int jpeg_parse(AVCodecParserContext *s,
  97. AVCodecContext *avctx,
  98. const uint8_t **poutbuf, int *poutbuf_size,
  99. const uint8_t *buf, int buf_size)
  100. {
  101. MJPEGParserContext *m = s->priv_data;
  102. ParseContext *pc = &m->pc;
  103. int next;
  104. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  105. next= buf_size;
  106. }else{
  107. next= find_frame_end(m, buf, buf_size);
  108. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  109. *poutbuf = NULL;
  110. *poutbuf_size = 0;
  111. return buf_size;
  112. }
  113. }
  114. *poutbuf = buf;
  115. *poutbuf_size = buf_size;
  116. return next;
  117. }
  118. AVCodecParser ff_mjpeg_parser = {
  119. .codec_ids = { AV_CODEC_ID_MJPEG, AV_CODEC_ID_JPEGLS },
  120. .priv_data_size = sizeof(MJPEGParserContext),
  121. .parser_parse = jpeg_parse,
  122. .parser_close = ff_parse_close,
  123. };