vf_framestep.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * filter to output only 1 every n frame, or only the I (key)frame
  3. *
  4. * The parameters are:
  5. *
  6. * [I] | [i]num
  7. *
  8. * if you call the filter with I (uppercase) as the parameter
  9. * ... -vf framestep=I ...
  10. * then ONLY the keyframes are outputted.
  11. * For DVD it means, generally, one every 15 frames (IBBPBBPBBPBBPBB), for avi it means
  12. * every scene change or every keyint value (see -lavcopts).
  13. *
  14. * if you call the filter with the i (lowercase)
  15. * ... -vf framestep=i ...
  16. * then a I! followed by a cr is printed when a key frame (eg Intra frame) is
  17. * found, leaving the current line of mplayer/mencoder, where you got the
  18. * time, in seconds, and frame of the key. Use this information to split the
  19. * AVI.
  20. *
  21. * After the i or alone you can put a positive number and only one frame every
  22. * x (the number you set) is passed on the filter chain, limiting the output
  23. * of the frame.
  24. *
  25. * Example
  26. * ... -vf framestep=i20 ...
  27. * Dump one every 20 frames, printing on the console when a I-Frame is encounter.
  28. *
  29. * ... -vf framestep=25
  30. * Dump one every 25 frames.
  31. *
  32. * If you call the filter without parameter it does nothing (except using memory
  33. * and resource of your system,. of course).
  34. *
  35. * This filter doesn' t work like the option -sstep seconds.
  36. *
  37. * The -sstep seek to the new position, without decoding all frames but,
  38. * expecially on avi file coded whith mpeg4 (lavc or xvid or divx), the
  39. * seek is not always too much precise.
  40. *
  41. * This filter simply discard the unwanted frames, so you are very precise in
  42. * counting the frame but sometime you use a lot of CPU for nothing.
  43. *
  44. * As usual it depends on what you're doing.
  45. *
  46. * copyright (c) 2003 Daniele Forghieri ( guru@digitalfantasy.it )
  47. *
  48. * This file is part of MPlayer.
  49. *
  50. * MPlayer is free software; you can redistribute it and/or modify
  51. * it under the terms of the GNU General Public License as published by
  52. * the Free Software Foundation; either version 2 of the License, or
  53. * (at your option) any later version.
  54. *
  55. * MPlayer is distributed in the hope that it will be useful,
  56. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  57. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  58. * GNU General Public License for more details.
  59. *
  60. * You should have received a copy of the GNU General Public License along
  61. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  62. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  63. */
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <string.h>
  67. #include "config.h"
  68. #include "mp_msg.h"
  69. #include "help_mp.h"
  70. #include "cpudetect.h"
  71. #include "img_format.h"
  72. #include "mp_image.h"
  73. #include "vf.h"
  74. /* Uncomment if you want to print some info on the format */
  75. // #define DUMP_FORMAT_DATA
  76. /* Private data */
  77. struct vf_priv_s {
  78. /* Current frame */
  79. int frame_cur;
  80. /* Frame output step, 0 = all */
  81. int frame_step;
  82. /* Only I-Frame (2), print on I-Frame (1) */
  83. int dump_iframe;
  84. };
  85. /* Filter handler */
  86. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  87. {
  88. mp_image_t *dmpi;
  89. struct vf_priv_s *priv;
  90. int skip;
  91. priv = vf->priv;
  92. /* Print the 'I' if is a intra frame. The \n advance the current line so you got the
  93. * current file time (in second) and the frame number on the console ;-)
  94. */
  95. if (priv->dump_iframe) {
  96. if (mpi->pict_type == 1) {
  97. mp_msg(MSGT_VFILTER, MSGL_INFO, "I!\n");
  98. }
  99. }
  100. /* decide if frame must be shown */
  101. if (priv->dump_iframe == 2) {
  102. /* Only key frame */
  103. skip = mpi->pict_type == 1 ? 0 : 1;
  104. }
  105. else {
  106. /* Only 1 every frame_step */
  107. skip = 0;
  108. if ((priv->frame_step != 0) && ((priv->frame_cur % priv->frame_step) != 0)) {
  109. skip = 1;
  110. }
  111. }
  112. /* Increment current frame */
  113. ++priv->frame_cur;
  114. if (skip == 0) {
  115. /* Get image, export type (we don't modify tghe image) */
  116. dmpi=vf_get_image(vf->next, mpi->imgfmt,
  117. MP_IMGTYPE_EXPORT, 0,
  118. mpi->w, mpi->h);
  119. /* Copy only the pointer ( MP_IMGTYPE_EXPORT ! ) */
  120. dmpi->planes[0] = mpi->planes[0];
  121. dmpi->planes[1] = mpi->planes[1];
  122. dmpi->planes[2] = mpi->planes[2];
  123. dmpi->stride[0] = mpi->stride[0];
  124. dmpi->stride[1] = mpi->stride[1];
  125. dmpi->stride[2] = mpi->stride[2];
  126. dmpi->width = mpi->width;
  127. dmpi->height = mpi->height;
  128. /* Chain to next filter / output ... */
  129. return vf_next_put_image(vf, dmpi, pts);
  130. }
  131. /* Skip the frame */
  132. return 0;
  133. }
  134. static void uninit(struct vf_instance *vf)
  135. {
  136. /* Free private data */
  137. free(vf->priv);
  138. }
  139. /* Main entry funct for the filter */
  140. static int vf_open(vf_instance_t *vf, char *args)
  141. {
  142. struct vf_priv_s *p;
  143. vf->put_image = put_image;
  144. vf->uninit = uninit;
  145. vf->default_reqs = VFCAP_ACCEPT_STRIDE;
  146. vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
  147. if (p == NULL) {
  148. return 0;
  149. }
  150. if (args != NULL) {
  151. #ifdef DUMP_FORMAT_DATA
  152. if (*args == 'd') {
  153. p->dump_iframe = 3;
  154. }
  155. else
  156. #endif
  157. if (*args == 'I') {
  158. /* Dump only KEY (ie INTRA) frame */
  159. p->dump_iframe = 2;
  160. }
  161. else {
  162. if (*args == 'i') {
  163. /* Print a 'I!' when a i-frame is encounter */
  164. p->dump_iframe = 1;
  165. ++args;
  166. }
  167. if (*args != '\0') {
  168. p->frame_step = atoi(args);
  169. if (p->frame_step <= 0) {
  170. mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_ErrorParsingArgument);
  171. return 0;
  172. }
  173. }
  174. }
  175. }
  176. return 1;
  177. }
  178. const vf_info_t vf_info_framestep = {
  179. "Dump one every n / key frames",
  180. "framestep",
  181. "Daniele Forghieri",
  182. "",
  183. vf_open,
  184. NULL
  185. };